简体   繁体   English

导入 Python 并在 C++ 中扩展

[英]Importing Python with extensions in C++

SOLVED: turns out I had a messed up path var in link script已解决:原来我在链接脚本中有一个混乱的路径变量

I have a problem concerning executing embedded python code in C++.我有一个关于在 C++ 中执行嵌入式 python 代码的问题。 I have a C++ program that dynamically executes a regular Python file using:我有一个 C++ 程序,它使用以下方法动态执行常规 Python 文件:

...
PyObject_CallObject(func, args);
...

Everything works as expected when I use plain function in the injected python scripts.当我在注入的 python 脚本中使用普通的 function 时,一切都按预期工作。 But when I want to import some extension in the python script:但是当我想在 python 脚本中导入一些扩展时:

import "libraryname"
... 

, I get the following runtime error: ,我收到以下运行时错误:

ImportError: No module named "libraryname"... ImportError:没有名为“libraryname”的模块...

The python code works using the regular interpreter but not ones it's injected in C++. python 代码使用常规解释器工作,但不是在 C++ 中注入的。 I've tried explicitly loading the python library folder both in the PATH var and in the linker without any success.我已经尝试在 PATH var 和 linker 中显式加载 python 库文件夹,但没有任何成功。

(I'm using Linux/Debian.) (我使用的是 Linux/Debian。)

Imports in Python don't use quotes around the name. Python 中的导入不要在名称周围使用引号。

import libraryname

Also, inject the module into sys.modules first.另外,首先将模块注入sys.modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM