cmake 发现错误的 python 库

发布于 2024-12-08 18:58:42 字数 1015 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野却迷人 2024-12-15 18:58:42

您可以通过指定 python 库的路径来告诉 cmake 在哪里找到此 PythonLibs,如下所示:

cmake -DPYTHON_LIBRARIES=/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib .

然后,这会将 cmake 内的 ${PYTHON_LIBRARIES} 设置为正确的路径。

要找出哪些其他可能的选项(除了 PYTHON_LIBRARIES 之外),您可以向 cmake(使用 -DARG 选项)尝试运行

ccmake .

然后按 c 进行配置,按 t 进行高级选项。

例如,您可能还想设置

-DPYTHON_LIBRARY='/softwarepath/Python/Python2.7/lib/libpython2.7.so'
-DPYTHON_INCLUDE='/softwarepath/Python/Python2.7/include'

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 (besides 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 might also want to set

-DPYTHON_LIBRARY='/softwarepath/Python/Python2.7/lib/libpython2.7.so'
-DPYTHON_INCLUDE='/softwarepath/Python/Python2.7/include'
情魔剑神 2024-12-15 18:58:42

解决找到错误版本的问题(例如 3.0 而不是 2.7)的最佳方法是为 find_package 指定最低版本(这将选择任何版本 >= 2.7):

FIND_PACKAGE(PythonLibs 2.7 REQUIRED)

或者获取确切的版本:

FIND_PACKAGE(PythonLibs 2.7.5 EXACT REQUIRED)

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):

FIND_PACKAGE(PythonLibs 2.7 REQUIRED)

or to get the exact version:

FIND_PACKAGE(PythonLibs 2.7.5 EXACT REQUIRED)
笑红尘 2024-12-15 18:58:42

您可以在 cmake libs \usr\share\cmake-3.2.3\Modules\FindPythonLibs.cmake 上手动设置:

set(PYTHON_LIBRARY "\\usr\\lib\\python2.7")
set(PYTHON_INCLUDE_DIR "\\usr\\include\\python2.7")

You can setup manually on cmake libs \usr\share\cmake-3.2.3\Modules\FindPythonLibs.cmake:

set(PYTHON_LIBRARY "\\usr\\lib\\python2.7")
set(PYTHON_INCLUDE_DIR "\\usr\\include\\python2.7")
吖咩 2024-12-15 18:58:42

当你这样做时,你就有效地将 python 嵌入到你的程序中。您是否在 PyRun_SimpleFile 之前调用了 Py_Initialize() ?查看在另一个应用程序中嵌入 Python

Py_Initialize() 将设置 sys.path,并且需要设置 python 环境。

如果您可以找到 python 的安装位置,则可以设置 python home 来覆盖 python 路径计算。在 Py_Initialize() 之前使用 Py_SetPythonHome()。

在 posix 类型的操作系统上,这里是 getpath.c 中的注释(路径解析的 cpython 实现):

/* Search in some common locations for the associated Python libraries.
 *
 * Two directories must be found, the platform independent directory
 * (prefix), containing the common .py and .pyc files, and the platform
 * dependent directory (exec_prefix), containing the shared library
 * modules.  Note that prefix and exec_prefix can be the same directory,
 * but for some installations, they are different.
 *
 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
 * Each search tries a number of different locations until a ``landmark''
 * file or directory is found.  If no prefix or exec_prefix is found, a
 * warning message is issued and the preprocessor defined PREFIX and
 * EXEC_PREFIX are used (even though they will not work); python carries on
 * as best as is possible, but most imports will fail.
 *
 * Before any searches are done, the location of the executable is
 * determined.  If argv[0] has one or more slashes in it, it is used
 * unchanged.  Otherwise, it must have been invoked from the shell's path,
 * so we search $PATH for the named executable and use that.  If the
 * executable was not found on $PATH (or there was no $PATH environment
 * variable), the original argv[0] string is used.
 *
 * Next, the executable location is examined to see if it is a symbolic
 * link.  If so, the link is chased (correctly interpreting a relative
 * pathname if one is found) and the directory of the link target is used.
 *
 * Finally, argv0_path is set to the directory containing the executable
 * (i.e. the last component is stripped).
 *
 * With argv0_path in hand, we perform a number of steps.  The same steps
 * are performed for prefix and for exec_prefix, but with a different
 * landmark.
 *
 * Step 1. Are we running python out of the build directory?  This is
 * checked by looking for a different kind of landmark relative to
 * argv0_path.  For prefix, the landmark's path is derived from the VPATH
 * preprocessor variable (taking into account that its value is almost, but
 * not quite, what we need).  For exec_prefix, the landmark is
 * Modules/Setup.  If the landmark is found, we're done.
 *
 * For the remaining steps, the prefix landmark will always be
 * lib/python$VERSION/os.py and the exec_prefix will always be
 * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
 * number as supplied by the Makefile.  Note that this means that no more
 * build directory checking is performed; if the first step did not find
 * the landmarks, the assumption is that python is running from an
 * installed setup.
 *
 * Step 2. See if the $PYTHONHOME environment variable points to the
 * installed location of the Python libraries.  If $PYTHONHOME is set, then
 * it points to prefix and exec_prefix.  $PYTHONHOME can be a single
 * directory, which is used for both, or the prefix and exec_prefix
 * directories separated by a colon.
 *
 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
 * backtracking up the path until it is exhausted.  This is the most common
 * step to succeed.  Note that if prefix and exec_prefix are different,
 * exec_prefix is more likely to be found; however if exec_prefix is a
 * subdirectory of prefix, both will be found.
 *
 * Step 4. Search the directories pointed to by the preprocessor variables
 * PREFIX and EXEC_PREFIX.  These are supplied by the Makefile but can be
 * passed in as options to the configure script.
 *
 * That's it!
 *
 * Well, almost.  Once we have determined prefix and exec_prefix, the
 * preprocessor variable PYTHONPATH is used to construct a path.  Each
 * relative path on PYTHONPATH is prefixed with prefix.  Then the directory
 * containing the shared library modules is appended.  The environment
 * variable $PYTHONPATH is inserted in front of it all.  Finally, the
 * prefix and exec_prefix globals are tweaked so they reflect the values
 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
 * off.  If either points to the build directory, the globals are reset to
 * the corresponding preprocessor variables (so sys.prefix will reflect the
 * installation location, even though sys.path points into the build
 * directory).  This seems to make more sense given that currently the only
 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
 * process to find the installed Python tree.
 */

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):

/* Search in some common locations for the associated Python libraries.
 *
 * Two directories must be found, the platform independent directory
 * (prefix), containing the common .py and .pyc files, and the platform
 * dependent directory (exec_prefix), containing the shared library
 * modules.  Note that prefix and exec_prefix can be the same directory,
 * but for some installations, they are different.
 *
 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
 * Each search tries a number of different locations until a ``landmark''
 * file or directory is found.  If no prefix or exec_prefix is found, a
 * warning message is issued and the preprocessor defined PREFIX and
 * EXEC_PREFIX are used (even though they will not work); python carries on
 * as best as is possible, but most imports will fail.
 *
 * Before any searches are done, the location of the executable is
 * determined.  If argv[0] has one or more slashes in it, it is used
 * unchanged.  Otherwise, it must have been invoked from the shell's path,
 * so we search $PATH for the named executable and use that.  If the
 * executable was not found on $PATH (or there was no $PATH environment
 * variable), the original argv[0] string is used.
 *
 * Next, the executable location is examined to see if it is a symbolic
 * link.  If so, the link is chased (correctly interpreting a relative
 * pathname if one is found) and the directory of the link target is used.
 *
 * Finally, argv0_path is set to the directory containing the executable
 * (i.e. the last component is stripped).
 *
 * With argv0_path in hand, we perform a number of steps.  The same steps
 * are performed for prefix and for exec_prefix, but with a different
 * landmark.
 *
 * Step 1. Are we running python out of the build directory?  This is
 * checked by looking for a different kind of landmark relative to
 * argv0_path.  For prefix, the landmark's path is derived from the VPATH
 * preprocessor variable (taking into account that its value is almost, but
 * not quite, what we need).  For exec_prefix, the landmark is
 * Modules/Setup.  If the landmark is found, we're done.
 *
 * For the remaining steps, the prefix landmark will always be
 * lib/python$VERSION/os.py and the exec_prefix will always be
 * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
 * number as supplied by the Makefile.  Note that this means that no more
 * build directory checking is performed; if the first step did not find
 * the landmarks, the assumption is that python is running from an
 * installed setup.
 *
 * Step 2. See if the $PYTHONHOME environment variable points to the
 * installed location of the Python libraries.  If $PYTHONHOME is set, then
 * it points to prefix and exec_prefix.  $PYTHONHOME can be a single
 * directory, which is used for both, or the prefix and exec_prefix
 * directories separated by a colon.
 *
 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
 * backtracking up the path until it is exhausted.  This is the most common
 * step to succeed.  Note that if prefix and exec_prefix are different,
 * exec_prefix is more likely to be found; however if exec_prefix is a
 * subdirectory of prefix, both will be found.
 *
 * Step 4. Search the directories pointed to by the preprocessor variables
 * PREFIX and EXEC_PREFIX.  These are supplied by the Makefile but can be
 * passed in as options to the configure script.
 *
 * That's it!
 *
 * Well, almost.  Once we have determined prefix and exec_prefix, the
 * preprocessor variable PYTHONPATH is used to construct a path.  Each
 * relative path on PYTHONPATH is prefixed with prefix.  Then the directory
 * containing the shared library modules is appended.  The environment
 * variable $PYTHONPATH is inserted in front of it all.  Finally, the
 * prefix and exec_prefix globals are tweaked so they reflect the values
 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
 * off.  If either points to the build directory, the globals are reset to
 * the corresponding preprocessor variables (so sys.prefix will reflect the
 * installation location, even though sys.path points into the build
 * directory).  This seems to make more sense given that currently the only
 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
 * process to find the installed Python tree.
 */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文