问题描述
我正在尝试将我们的 Python 版本从 3.4 更新到 3.6.我们将 Python 嵌入到 C++ 应用程序中,因此使用新的(自 Python 3.5 起)Windows x86 可嵌入 zip 文件.但是,我们的应用程序无法执行,因为lib/site-packages"没有被添加到 sys.path
变量中.我已经确认在我的机器上安装 Python 3.6 并从安装位置运行后,sys.path
变量包含(相对于 python 目录):
I am attempting to update our Python version from 3.4 to 3.6. We are embedding Python into a C++ application, so it seemed logical to utilize the new (since Python 3.5) Windows x86 embeddable zip file. However, our application is failing to execute because "lib/site-packages" isn't being added to the sys.path
variable. I have confirmed that after installing Python 3.6 on my machine, and running from the installed location, the sys.path
variable contains (relative to the python directory):
'...\\python36.zip'
'...\\DLLs'
'...\\lib'
'...'
'...\\lib\\site-packages'
但是,当我在同一台机器上从可嵌入的 zip 文件运行时,sys.path
变量包含(相对于 python 目录):
However, when I run from the embeddable zip file on the same machine, the sys.path
variable contains (relative to the python directory):
'...\\python36.zip'
'...'
'...\\\n'
在这两种情况下,lib/site-packages"目录都存在于 Python 目录中.我也没有定义了 PYTHONPATH 环境变量.关于它如何确定 sys.path
,有谁知道如何让可嵌入的 zip 文件与已安装的版本相同?
In both cases, the "lib/site-packages" directory exists within the Python directory. I also don't have a PYTHONPATH environment variable defined. Does anyone know how to get the embeddable zip file to act the same as the installed version, with respect to how it determines sys.path
?
推荐答案
解压 Python 可嵌入 zip 文件后,有一个名为python36._pth
在根目录中.该文件包含以下文本:
After extracting the Python embeddable zip file, there is a file calledpython36._pth
in the root directory. That file contains the following text:
# Uncomment to run site.main() automatically
#import site
如注释所示,只需取消注释 import site
语句删除#"字符.这样做之后,sys.path
变量包含:
As the comment indicates, simply uncomment the import site
statement byremoving the '#' character. After doing so, the sys.path
variable contains:
'...\\python36.zip'
'...'
'...\\\n'
'...\\lib\\site-packages'
这仍然与安装的版本不同,但正是我在我的特殊情况下需要.
This is still different than the installed version, but is exactly what Ineeded in my particular case.
开始编辑
我还发现您可以完全删除 python36._pth
文件,这将 Python 恢复为不可嵌入版本的行为.
I also discovered that you can remove the python36._pth
file entirely, whichreverts Python to behavior of the non-embeddable version.
这篇关于Python 可嵌入 Zip 文件在 sys.path 中不包含 lib/site-packages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!