提升 Python 如何在导入的 Python 文件中导入 py
boost python how to import py in imported python file
in my main.cpp:
bp::object main = bp::import("__main__");
bp::object globals = main.attr("__dict__");
bp::object module = import("strategy", "strategy.py", globals);
bp::object Strategy = module.attr("Strategy");
bp::object strategy = Strategy();
但我想导入一些自定义的 py 文件,strategy.py 我有:
from model import Model
当我运行 ./main 时,它给出错误:
File "strategy.py", line 2, in <module>
from model import Model
ImportError: No module named model
当我在没有 boost python 的情况下测试 strategy.py 时,导入运行良好,我该如何解决它?
感谢 Dan,我最终得到了这样的东西来自动将当前目录复制到系统路径中:
bp::object sys_module = bp::import("sys");
bp::str module_directory = getDir().c_str();
sys_module.attr("path").attr("insert")(1, module_directory);
getDir() 函数:
std::string getDir() // get current directory path
{
char cCurrentPath[FILENAME_MAX];
if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath))){
return "";
}
cCurrentPath[sizeof(cCurrentPath) - 1] = ' ';
return std::string(cCurrentPath);
}
相关文章:
- Python 3.8 不能与 Pybind11 一起导入 [Windows]
- 在 python 模块中导入子模块时PyImport_Import失败
- 在从Qt调用的Python脚本中导入OpenCV崩溃
- 编译boost_python扩展无法在 python 2.7 中导入
- Python 3.8:运行外部C++代码:无法导入模块
- Python/C++:可以导入犰狳(arma::)但不能导入子程序arma::arma_rng::randn
- 将 c++-Programm 的结果导入 python
- 如何使用Java Native Interface在C++中导入python库-Android Studio
- 将两个使用C++流的共享库导入 python 会导致输出损坏
- 导入Python库时,嵌入了Python代码错误
- 直接导入 python 代码中的头文件
- 将.cpp文件中的函数导入python
- 如何从 .so 文件导入 python 模块
- 导入 Python 模块,该模块在 C++ 中使用 GCC 4.4.3 编译
- 从 C++ 中的 PATH 导入 Python 脚本
- 无法在Ubuntu上导入Python中的FANN
- 导入python包时找不到Armadillo库(OS X, python 2.7)
- 如何将c++程序编译到要导入Python的共享库中
- 在GLIBC版本较低的目标系统上,从.so文件导入Python模块失败
- 导入C++模块,如果失败:导入 Python 版本