【发布时间】:2012-06-19 06:22:27
【问题描述】:
我有一个模块modA,其中包含一个合成子模块modB(用PyModule_New创建);现在导入模块:
-
from modA import modB没关系 -
import modA.modB失败。
我错过了什么?
-
modA.cpp(使用
boost::python,但很可能与纯python的c-API相同):#include<boost/python.hpp> namespace py=boost::python; BOOST_PYTHON_MODULE(modA){ py::object modB=py::object(py::handle<>(PyModule_New("modB"))); modB.attr("__file__")="<synthetic>"; py::scope().attr("modB")=modB; }; -
使用(g++ 而不是 clang++ 的编译方式相同)
clang++ -o modA.so modA.cpp -fPIC -shared -lboost_python `pkg-config python --cflags --libs` -
test.py:
import sys sys.path.append('.') from modA import modB import modA.modB -
python test.py(注意第一次导入没问题,第二次导入失败):Traceback (most recent call last): File "test.py", line 4, in <module> import modA.modB ImportError: No module named modB
【问题讨论】:
-
您应该添加您的解决方案作为答案并接受它。
-
我刚做了,谢谢你的建议。必须等待 2 天才能接受它:-)
标签: python boost-python python-module synthetic