编辑:我在这里问了一个相反的问题:How to embed Python3 with the standard library
此处提供了Python2的解决方案:Is it possible to embed python without the standard library?
但是,Python3无法在Py_Initialize();
上执行以下操作:
Fatal Python error: Py_Initialize: unable to load the file system
codec ImportError: No module named 'encodings'
这是有道理的,因为py3源文件默认为utf-8。因此,似乎只需要外部二进制文件即可解析py3源文件。
那么该怎么办?
似乎我需要在系统Python安装中找到
encodings
二进制文件,将其复制到项目树中,并可能设置一些环境变量PYTHONPATH(?),以便我的libpython.dylib可以找到它。有可能避免这种情况吗?如果没有,谁能澄清我需要采取的步骤?还会有更多的打?吗?
注意:为了后代,这是我如何将独立的libpython.dylib链接到OSX上的项目:
首先,我找到系统Python的库:
/usr/local/Frameworks/Python.framework/Versions/3.4/Python
(在我的情况下,它是通过Homebrew安装的)。现在我:
./Libs/libpython3.4.1_OSX.dylib
build settings -> linking
并将other linker flags
设置为-lpython3.4.1_OSX
此时,它似乎可以正常工作。但是,如果您知道尝试在全新的OSX安装上构建它,它将失败。这是因为:
$ otool -D ./libpython3.4.1_OSX.dylib
./libpython3.4.1_OSX.dylib:
/usr/local/Frameworks/Python.framework/Versions/3.4/Python
.dylib仍保留其旧位置。我真的很奇怪,.dylib包含一个指向其位置的链接,因为使用它的任何人都必须知道它在哪里才能首先调用它!
我们可以通过以下方式纠正此问题:
$ install_name_tool -id @rpath/libpython3.4.1_OSX.dylib libpython3.4.1_OSX.dylib
但是,在我们的Xcode项目中,还必须:
build phases
。添加一个copy files
步骤,将libpython3.4.1_OSX.dylib
复制到Frameworks
(在正确的位置放置它)。 build settings -> linking
并将runpath search paths
设置为@executable_path/../Frameworks/libpython3.4.1_OSX.dylib
最后,我需要进入
edit scheme -> run -> arguments -> environment variables
并添加值为PYTHONHOME
的../Frameworks
我怀疑要使其正常工作,我还需要添加一个
PYTHONPATH
链接:
https://mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html
http://qin.laya.com/tech_coding_help/dylib_linking.html
https://github.com/conda/conda-build/issues/279#issuecomment-67241554
Can you please help me understand how Mach-O libraries work in Mac Os X?
http://nshipster.com/launch-arguments-and-environment-variables/
最佳答案
我已经尝试过了,这比没有Python库嵌入Python 3要花费更多的时间。
库中的某些模块对于运行Python 3是必不可少的,如果没有它们,则需要进行大量修改才能使其运行。
关于python - 嵌入不带标准库的Python3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34724057/