我正在尝试使用默认的Python freeze.py
实用程序将Python 3.6.5应用程序冻结到Linux(Fedora 27 64位)上的独立可执行文件中。我已经编译了Python 3.6.5,我试图用以下(清理过的)命令冻结我的应用程序:
LOCAL_PYTHON=(path to my compiled local python directory)
$LOCAL_PYTHON/python $LOCAL_PYTHON/Tools/freeze/freeze.py -p $LOCAL_PYTHON
-P $LOCAL_PYTHON -o frozen myapp.py
此冻结命令似乎工作正常,并按我的预期生成myapp
可执行文件,但运行可执行文件会引发以下异常:
Failed to import the site module
Traceback (most recent call last):
File "$LOCAL_PYTHON/Lib/importlib/_bootstrap.py", line 971, in _find_and_load
return _find_and_load_unlocked(name, import_)
File "$LOCAL_PYTHON/Lib/importlib/_bootstrap.py", line 955, in _find_and_load_unlocked
module = _load_unlocked(spec)
File "$LOCAL_PYTHON/Lib/importlib/_bootstrap.py", line 665, in _load_unlocked
spec.loader.exec_module(module)
File "$LOCAL_PYTHON/Lib/importlib/_bootstrap.py", line 807, in exec_module
exec(code, module.__dict__)
File "$LOCAL_PYTHON/Lib/site.py", line 544, in <module>
main()
File "$LOCAL_PYTHON/Lib/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "$LOCAL_PYTHON/Lib/site.py", line 282, in addusersitepackages
user_site = getusersitepackages()
File "$LOCAL_PYTHON/Lib/site.py", line 258, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "$LOCAL_PYTHON/Lib/site.py", line 248, in getuserbase
USER_BASE = get_config_var('userbase')
File "$LOCAL_PYTHON/Lib/sysconfig.py", line 601, in get_config_var
return get_config_vars().get(name)
File "$LOCAL_PYTHON/Lib/sysconfig.py", line 550, in get_config_vars
_init_posix(_CONFIG_VARS)
File "$LOCAL_PYTHON/Lib/sysconfig.py", line 421, in _init_posix
_temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
File "$LOCAL_PYTHON/Lib/importlib/_bootstrap.py", line 971, in _find_and_load
return _find_and_load_unlocked(name, import_)
File "$LOCAL_PYTHON/Lib/importlib/_bootstrap.py", line 953, in _find_and_load_unlocked
raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
ModuleNotFoundError: No module named '_sysconfigdata_m_linux_x86_64-linux-gnu'
如果我运行它没有冻结(例如$LOCAL_PYTHON/python myapp.py
),我的应用程序工作正常,并且相同的冻结命令和应用程序在Windows上生成可运行的可执行文件。是否存在某些特定于平台的冻结参数或环境变量?
答案 0 :(得分:2)
所以我能够通过使用docker重现这个问题。我使用下面的
运行了一个docker容器return this.addMessage.controls['message'].value
然后运行以下命令从源代码
设置pythondocker run -it fedora:27 bash
之后我创建了一个简单的yum install -y git gcc unzip zip wget curl make cmake zlib-devel libsq3-devel readline-devel openssl-devel which vim findutils
mkdir py
cd py/
wget "https://github.com/python/cpython/archive/v3.6.5.zip"
unzip v3.6.5.zip
cd cpython-3.6.5/
./configure --with-debug
make -j8
app.py
然后我跑了
print('this is from app')
我得到了以下异常
export LANG=en_US.UTF-8
LOCAL_PYTHON=/py/cpython-3.6.5/
$LOCAL_PYTHON/python $LOCAL_PYTHON/Tools/freeze/freeze.py -p $LOCAL_PYTHON -P $LOCAL_PYTHON -o frozen app.py
cd frozen/
make
./app
和你一样。然后我运行[root@54ba4757d177 frozen]# ./app
Failed to import the site module
Traceback (most recent call last):
File "/py/cpython-3.6.5/Lib/importlib/_bootstrap.py", line 971, in _find_and_load
return _find_and_load_unlocked(name, import_)
File "/py/cpython-3.6.5/Lib/importlib/_bootstrap.py", line 955, in _find_and_load_unlocked
module = _load_unlocked(spec)
File "/py/cpython-3.6.5/Lib/importlib/_bootstrap.py", line 665, in _load_unlocked
spec.loader.exec_module(module)
File "/py/cpython-3.6.5/Lib/importlib/_bootstrap.py", line 807, in exec_module
exec(code, module.__dict__)
File "/py/cpython-3.6.5/Lib/site.py", line 544, in <module>
main()
File "/py/cpython-3.6.5/Lib/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/py/cpython-3.6.5/Lib/site.py", line 282, in addusersitepackages
user_site = getusersitepackages()
File "/py/cpython-3.6.5/Lib/site.py", line 258, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/py/cpython-3.6.5/Lib/site.py", line 248, in getuserbase
USER_BASE = get_config_var('userbase')
File "/py/cpython-3.6.5/Lib/sysconfig.py", line 601, in get_config_var
return get_config_vars().get(name)
File "/py/cpython-3.6.5/Lib/sysconfig.py", line 550, in get_config_vars
_init_posix(_CONFIG_VARS)
File "/py/cpython-3.6.5/Lib/sysconfig.py", line 421, in _init_posix
_temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
File "/py/cpython-3.6.5/Lib/importlib/_bootstrap.py", line 971, in _find_and_load
return _find_and_load_unlocked(name, import_)
File "/py/cpython-3.6.5/Lib/importlib/_bootstrap.py", line 953, in _find_and_load_unlocked
raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
ModuleNotFoundError: No module named '_sysconfigdata_m_linux_x86_64-linux-gnu'
来安装python并再次尝试
make install
然后再次使用该应用
make install -C /py/cpython-3.6.5/
它有效,所以我明白问题是生成的应用程序假定python安装在机器上。然后我以
的形式找到了黄金Python freeze.py generated bin doesn't run
我更新了[root@54ba4757d177 frozen]# ./app
this is from app
文件,并在/py/cpython-3.6.5/Tools/freeze/makefreeze.py
之前添加了Py_NoSiteFlag = 1;
。
当然我再次创建容器以确保系统上没有安装python。然后再次重新编译应用程序和宾果游戏!
PyImport_FrozenModules = _PyImport_FrozenModules;
为了更进一步,我删除了所有python源代码,构建文件并再次运行应用程序,它运行良好