尝试冻结Python 3.6.5应用程序时_sysconfigdata错误 [英] _sysconfigdata error when trying to Freeze a Python 3.6.5 application

查看:127
本文介绍了尝试冻结Python 3.6.5应用程序时_sysconfigdata错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用默认的Python freeze.py实用程序将Python 3.6.5应用程序冻结为Linux(Fedora 27 64位)上的独立可执行文件.我已检出已编译的Python 3.6.5,并且正在尝试使用以下(经过清理)的命令冻结我的应用程序:

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

此冻结命令似乎工作正常,并按我的期望生成了myapp可执行文件,但是运行该可执行文件会引发以下异常:

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'  

如果我不冻结地运行它(例如$LOCAL_PYTHON/python myapp.py),我的应用程序就可以正常运行,并且相同的冻结命令和应用程序会在Windows上生成一个可运行的可执行文件.我缺少某些特定于平台的冻结参数或环境变量吗?

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?

推荐答案

因此,我能够使用docker重现此问题.我在下面运行了一个Docker容器

So I was able to reproduce the issue by using docker. I ran a docker container using below

docker run -it fedora:27 bash

然后运行以下命令以从源代码设置python

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

之后,我创建了一个简单的app.py

After that I created a simple 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'

就像你得到的一样.然后我运行make install安装python并再次尝试

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

它起作用了,所以我了解到问题在于生成的应用程序假定python已安装在计算机上.然后我发现了

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 Frozen.py生成的bin无法运行

我更新了/py/cpython-3.6.5/Tools/freeze/makefreeze.py文件,并在PyImport_FrozenModules = _PyImport_FrozenModules;之前添加了Py_NoSiteFlag = 1;.

I updated the /py/cpython-3.6.5/Tools/freeze/makefreeze.py file and added Py_NoSiteFlag = 1; before PyImport_FrozenModules = _PyImport_FrozenModules;.

当然,我再次创建了容器以确保系统上未安装python.然后再次重新编译应用程序并进行宾果游戏!

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

为了更进一步,我删除了所有python源代码,构建文件并再次运行应用程序,效果很好

To go one step further, I deleted all python source, build files and ran app again and it worked great

这篇关于尝试冻结Python 3.6.5应用程序时_sysconfigdata错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆