问题描述
我是从c ++程序调用python脚本:
#include< Python.h>
...
Py_Initialize();
PyRun_SimpleFile(...);
Py_Finalize();
我的cmake文件中对应的cmake条目是:
FIND_PACKAGE(PythonLibs REQUIRED)
pre>
...
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。 7.dylib - 我在mac),这是如何获得标准的python模块,但没有一个我自己安装。要将PYTHONPATH更改回正常,我添加了
尝试:
import some_package
b $ b如果sys.path中的my_python_path:raise
sys.path.append(my_python_path)
你可以告诉cmake在哪里可以找到这个PythonLibs通过指定一个python脚本的顶部。
解决方案python库的路径如下:
cmake -DPYTHON_LIBRARIES = / Library / Frameworks / Python.framework / lib / libpython2.7.dylib。
这样会将cmake中的$ {PYTHON_LIBRARIES}设置为正确的路径。
要找出哪些其他可能的选项(PYTHON_LIBRARIES),您可以给cmake(使用-DARG选项)尝试运行
ccmake。
然后按
c
code> t 用于高级选项。
例如,您也可以设置
-DPYTHON_LIBRARY ='/ softwarepath / Python / Python2.7 / lib / libpython2.7.so'
-DPYTHON_INCLUDE ='/ softwarepath / Python / Python2 .7 / include'
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.
解决方案you can tell cmake where to find this PythonLibs by specifying the path to your python libraries like this:
cmake -DPYTHON_LIBRARIES=/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib .
this will then set the ${PYTHON_LIBRARIES} inside cmake to the right path.
To find out which other possible options (besided PYTHON_LIBRARIES) you can give to cmake (with the -DARG option) try running
ccmake .
Then press
c
to configure, andt
for advanced options.For example, you also might want to set
-DPYTHON_LIBRARY='/softwarepath/Python/Python2.7/lib/libpython2.7.so' -DPYTHON_INCLUDE='/softwarepath/Python/Python2.7/include'
这篇关于cmake发现错误的python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!