问题
I'm trying to Freeze a Python 3.6.5 app into a standalone executable on Linux (Fedora 27 64 bit), using the default Python freeze.py
utility. I have a checkout of Python 3.6.5 compiled, and I'm trying to freeze my application with the following (sanitized) commands:
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
This freeze command seems to work fine, and generates the myapp
executable as I would expect, but running the executable throws the following exception:
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'
My application works fine if I run it without freezing (e.g. $LOCAL_PYTHON/python myapp.py
), and the same freeze command and app generate a working executable on Windows. Are there some platform specific Freeze parameters or environment variables that I'm missing?
回答1:
So I was able to reproduce the issue by using docker. I ran a docker container using below
docker run -it fedora:27 bash
And then ran below commands to setup python from source
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
After that I created a simple app.py
print('this is from app')
And then I ran
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
And I got below exception
[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'
Same like you got. Then I ran make install
to install python and tried again
make install -C /py/cpython-3.6.5/
And then the app again
[root@54ba4757d177 frozen]# ./app
this is from app
And it worked, so I understood that the issue is that the generated app assumes python is installed on machine. And then I found Gold in the form of
Python freeze.py generated bin doesn't run
I updated the /py/cpython-3.6.5/Tools/freeze/makefreeze.py
file and added Py_NoSiteFlag = 1;
before PyImport_FrozenModules = _PyImport_FrozenModules;
.
Of course I created the container again to make sure no python was installed on system. And then recompiled the app again and bingo!
[root@06284cc1ae0c frozen]# ./app
this is from app
To go one step further, I deleted all python source, build files and ran app again and it worked great
来源:https://stackoverflow.com/questions/49921336/sysconfigdata-error-when-trying-to-freeze-a-python-3-6-5-application