python错误提示库没有注册_cmake发现错误的python库

I'm new to CMake and have trouble understanding some usage concepts.

I'm calling a python script from a c++ program:

#include

...

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, and t 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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值