cmake 发现错误的 python 库
我是 CMake 新手,无法理解一些使用概念。
我正在从 C++ 程序调用 python 脚本:
#include <Python.h>
...
Py_Initialize();
PyRun_SimpleFile(...);
Py_Finalize();
cmake 文件中相应的 cmake 条目是:
FIND_PACKAGE(PythonLibs REQUIRED)
...
TARGET_LINK_LIBRARIES(MyApplication ${PYTHON_LIBRARIES})
只要我的 python 脚本不使用安装到 site-packages 目录中的任何模块,此方法就有效,否则我会收到 ImportError。 此问题展示了如何查找 site-packages 目录的位置使用 CMake,但是我应该告诉 CMake 来做什么呢?
编辑:问题已解决。结果 FIND_PACKAGE(PythonLibs) 找到了与我通常使用的不同的 python 安装(/usr/local/lib/libpython2.7.dylib 而不是 /Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.dylib) 7.dylib - 我在 mac 上),这就是我获取标准 python 模块的方式,但没有一个是我自己安装的。为了将 PYTHONPATH 更改回正常状态,我
try:
import some_package
except ImportError:
if "my_python_path" in sys.path: raise
sys.path.append("my_python_path")
在 python 脚本的顶部添加了以下内容。
I'm new to CMake and have trouble understanding some usage concepts.
I'm calling a python script from a c++ program:
#include <Python.h>
...
Py_Initialize();
PyRun_SimpleFile(...);
Py_Finalize();
The corresponding cmake entries in my cmake file are:
FIND_PACKAGE(PythonLibs REQUIRED)
...
TARGET_LINK_LIBRARIES(MyApplication ${PYTHON_LIBRARIES})
This works as long as my python script isn't using any modules installed into the site-packages directory, otherwise I get an ImportError. This question shows how to find the location of the site-packages directory with CMake, but what should I tell CMake to do with it?
EDIT: Problem solved. Turns out FIND_PACKAGE(PythonLibs) finds a different python installation from what I'm normally using (/usr/local/lib/libpython2.7.dylib instead of /Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib - I'm on mac), which is how I get standard python modules, but none that I installed myself. To change the PYTHONPATH back to normal, I added
try:
import some_package
except ImportError:
if "my_python_path" in sys.path: raise
sys.path.append("my_python_path")
at the top of my python script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过指定 python 库的路径来告诉 cmake 在哪里找到此 PythonLibs,如下所示:
然后,这会将 cmake 内的 ${PYTHON_LIBRARIES} 设置为正确的路径。
要找出哪些其他可能的选项(除了 PYTHON_LIBRARIES 之外),您可以向 cmake(使用 -DARG 选项)尝试运行
然后按
c
进行配置,按t
进行高级选项。例如,您可能还想设置
You can tell cmake where to find this PythonLibs by specifying the path to your python libraries like this:
This will then set the ${PYTHON_LIBRARIES} inside cmake to the right path.
To find out which other possible options (besides PYTHON_LIBRARIES) you can give to cmake (with the -DARG option) try running
Then press
c
to configure, andt
for advanced options.For example, you might also want to set
解决找到错误版本的问题(例如 3.0 而不是 2.7)的最佳方法是为 find_package 指定最低版本(这将选择任何版本 >= 2.7):
或者获取确切的版本:
The best way to solve the problem that the wrong version is found (for instance 3.0 instead of 2.7) is to specify the minimum version to find_package (this will choose any version >= 2.7):
or to get the exact version:
您可以在 cmake libs
\usr\share\cmake-3.2.3\Modules\FindPythonLibs.cmake
上手动设置:You can setup manually on cmake libs
\usr\share\cmake-3.2.3\Modules\FindPythonLibs.cmake
:当你这样做时,你就有效地将 python 嵌入到你的程序中。您是否在 PyRun_SimpleFile 之前调用了 Py_Initialize() ?查看在另一个应用程序中嵌入 Python。
Py_Initialize() 将设置 sys.path,并且需要设置 python 环境。
如果您可以找到 python 的安装位置,则可以设置 python home 来覆盖 python 路径计算。在 Py_Initialize() 之前使用 Py_SetPythonHome()。
在 posix 类型的操作系统上,这里是 getpath.c 中的注释(路径解析的 cpython 实现):
You are effectively embedding python in your program when you do this. Did you call Py_Initialize() before PyRun_SimpleFile ? Have a look at Embedding Python in Another Application.
Py_Initialize() will set up sys.path and is required to set the python environment.
If you can find out where python is installed, it is possible to set python home to override python path calculations. Use Py_SetPythonHome() before Py_Initialize().
On posix type OSes, here is a comment in getpath.c (cpython implementation of path resolving):