我试图导入一个python模块并在C++中执行一个函数。我有以下代码片段:
int python_exec(const fs::path& module_path, std::string_view function) {
const auto tmp = module_path.string();
PyObject* module = PyImport_ImportModule(tmp.data());
if (!module) {
return -1;
}
PyObject* symbol = PyObject_GetAttrString(module, function.data());
if (!symbol) {
return - 1;
}
if (PyCallable_Check(symbol)) {
PyObject* value = PyObject_CallObject(symbol, nullptr);
return PyLong_AsLong(value);
}
return -1;
}
int main(int argc, char** argv)
{
const auto current = fs::current_path();
const auto module = argv[1];
std::cout << current << '\n'; // "C:\\full\\path\\to\\project\\cmake-build-debug"
std::cout << module << '\n'; // "../test/test.py"
Py_Initialize();
Py_SetPath(current.c_str());
std::cout << python_exec(module, "my_func") << '\n';
Py_Finalize();
return 0;
}
以及以下项目结构:
project
|_cmake-build-debug
| |_app.exe
|_test
|_test.py
我的可执行文件在build目录中运行,我将"../test/test.py"
作为程序参数传递
Python抛出ModuleNotFoundError: No module named '.'
错误。
我还尝试在Py_Initialize
之前调用Py_SetPath
,如这里所建议的https://stackoverflow.com/a/63452021/22124714,但这导致了其他错误,我无法完全解释:
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\full\\path\\to\\project\\cmake-build-debug\\pytest.exe'
sys.base_prefix = ''
sys.base_exec_prefix = ''
sys.platlibdir = 'lib'
sys.executable = 'C:\\full\\path\\to\\project\\cmake-build-debug\\pytest.exe'
sys.prefix = ''
sys.exec_prefix = ''
sys.path = [
'C:\\full\\path\\to\\project\\cmake-build-debug',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
这两条线看起来很可疑。
PYTHONHOME = (not set)
PYTHONPATH = (not set)
我想我没有正确理解Py_SetPath
函数,如何解决这个问题?
1条答案
按热度按时间twh00eeo1#
这个问题是关于Python用来查找模块的路径,也就是sys.path
设置路径的方法有多种:
PySys_SetPath
PyList_Append
PyRun_SimpleString()
注意事项:
PyImport_ImportModule
只取模块名,省略文件扩展名。