【问题标题】:import error in embedding python with boost python使用boost python嵌入python时导入错误
【发布时间】:2014-02-06 06:12:29
【问题描述】:

我有以下代码

#include <boost/python.hpp>

int main()
{
    Py_Initialize();
    namespace python = boost::python;
    try {
        python::object main = python::import("sample");
    } catch(...) {
        PyErr_Print();
        PyErr_Clear();
    }
}

我收到以下错误:

ImportError: No module named sample

我将我的 sample.py 放在与该程序相同的目录中。

【问题讨论】:

  • 调试导入问题时,将PYTHONVERBOSE 环境变量设置为2 会很有帮助。这将导致 Python 打印 python 尝试导入文件的位置。

标签: c++ boost-python


【解决方案1】:

这是因为 python::import 没有在当前目录中查找。我知道两种解决方法:

设置 PYTHONPATH 以查看当前目录 (linux):

export PYTHONPATH=`pwd`:$PYTHONPATH

或者...

在您的代码中设置 python 搜索模块路径(它还为您发现的问题提供了更好的解释): How does import work with Boost.Python from inside python files

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 2020-11-19
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多