【问题标题】:Got exception boost::python::error_already_set on boost::python::import("cv2")在 boost::python::import("cv2") 上得到异常 boost::python::error_already_set
【发布时间】:2019-11-17 17:46:44
【问题描述】:

我在调用 boost::python::import("cv2") 时抛出异常 boost::python::error_already_set

首先,我写了一个python脚本pymod.py

import sys
...

并在 cpp 代码中导入 pymod.pyboost::python

...
boost::python::import("pymod");
...

效果很好。

但是当我在pymod.py 中添加cv2 时:

import sys, cv2

boost::python::import("pymod") 抛出异常:

terminate called after throwing an instance of 'boost::python::error_already_set'

注意import cv2的python脚本可以被python调用:

python pymod.py

没有错误。

所以我尝试了使用boost::python 导入cv2 的cpp 版本,并抛出了相同的异常。代码如下:

#include <boost/dll/import.hpp>
#include <boost/python.hpp>

int main(int nArgCnt, char *ppArgs[]) {
    Py_Initialize();
    boost::python::object pySys = boost::python::import("cv2");
    return 0;
}


更新 1:

我也查了boost_1_65_1/libs/python/src/import.cpp的源码,发现boost::python::import的实现:

object BOOST_PYTHON_DECL import(str name)
{
  // should be 'char const *' but older python versions don't use 'const' yet.
  char *n = python::extract<char *>(name);
  python::handle<> module(PyImport_ImportModule(n));
  return python::object(module);
}

而我尝试直接使用PyImport_ImportModule,但也失败了:

#include <boost/dll/import.hpp>
#include <boost/python.hpp>
#include <Python.h>
#include <iostream>

int main(int nArgCnt, char *ppArgs[]) {
    Py_Initialize();
    // numpy, sys and other modules can be load! VERY STRANGE!!
    auto pMod = PyImport_ImportModule("cv2");
    std::cout << pMod << std::endl;
    return 0;
}

此代码打印以下输出:

['/usr/local/lib/python3.6/dist-packages', '/usr/local/lib/python3.6/dist-packages/cv2/python-3.6', '/home/devymex/3rdparty/opencv-4.0.1/build/python_loader', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/devymex/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages/mxnet-1.3.1-py3.6.egg', '/usr/local/lib/python3.6/dist-packages/graphviz-0.8.4-py3.6.egg', '/usr/local/lib/python3.6/dist-packages/typing-3.6.6-py3.6.egg', '/usr/local/lib/python3.6/dist-packages/typing_extensions-3.6.6-py3.6.egg', '/usr/local/lib/python3.6/dist-packages/onnx-1.5.0-py3.6-linux-x86_64.egg', '/usr/lib/python3/dist-packages']
0

但我很确定命令python3 -c "import cv2" 没有问题。所以它似乎与提升无关。


更新 2:

PyErr_Print(); 提供以下信息:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/cv2/__init__.py", line 89, in <module>
    bootstrap()
  File "/usr/local/lib/python3.6/dist-packages/cv2/__init__.py", line 79, in bootstrap
    import cv2
  File "/usr/local/lib/python3.6/dist-packages/cv2/__init__.py", line 89, in <module>
    bootstrap()
  File "/usr/local/lib/python3.6/dist-packages/cv2/__init__.py", line 23, in bootstrap
    raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.

__init__.py:

'''
OpenCV Python binary extension loader
'''
import os
import sys

try:
    import numpy
    import numpy.core.multiarray
except ImportError:
    print('OpenCV bindings requires "numpy" package.')
    print('Install it via command:')
    print('    pip install numpy')
    raise

# TODO
# is_x64 = sys.maxsize > 2**32

def bootstrap():
    import sys
    if hasattr(sys, 'OpenCV_LOADER'):
        print(sys.path)
        raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
    sys.OpenCV_LOADER = True

    DEBUG = False
    if hasattr(sys, 'OpenCV_LOADER_DEBUG'):
        DEBUG = True

    import platform
    if DEBUG: print('OpenCV loader: os.name="{}"  platform.system()="{}"'.format(os.name, str(platform.system())))

    LOADER_DIR=os.path.dirname(os.path.abspath(__file__))

    PYTHON_EXTENSIONS_PATHS = []
    BINARIES_PATHS = []

    g_vars = globals()
    l_vars = locals()

    if sys.version_info[:2] < (3, 0):
        from cv2.load_config_py2 import exec_file_wrapper
    else:
        from . load_config_py3 import exec_file_wrapper

    def load_first_config(fnames, required=True):
        for fname in fnames:
            fpath = os.path.join(LOADER_DIR, fname)
            if not os.path.exists(fpath):
                if DEBUG: print('OpenCV loader: config not found, skip: {}'.format(fpath))
                continue
            if DEBUG: print('OpenCV loader: loading config: {}'.format(fpath))
            exec_file_wrapper(fpath, g_vars, l_vars)
            return True
        if required:
            raise ImportError('OpenCV loader: missing configuration file: {}. Check OpenCV installation.'.format(fnames))

    load_first_config(['config.py'], True)
    load_first_config([
        'config-{}.{}.py'.format(sys.version_info[0], sys.version_info[1]),
        'config-{}.py'.format(sys.version_info[0])
    ], True)

    if DEBUG: print('OpenCV loader: PYTHON_EXTENSIONS_PATHS={}'.format(str(l_vars['PYTHON_EXTENSIONS_PATHS'])))
    if DEBUG: print('OpenCV loader: BINARIES_PATHS={}'.format(str(l_vars['BINARIES_PATHS'])))

    for p in reversed(l_vars['PYTHON_EXTENSIONS_PATHS']):
        sys.path.insert(1, p)

    if os.name == 'nt':
        os.environ['PATH'] = ';'.join(l_vars['BINARIES_PATHS']) + ';' + os.environ.get('PATH', '')
        if DEBUG: print('OpenCV loader: PATH={}'.format(str(os.environ['PATH'])))
    else:
        # amending of LD_LIBRARY_PATH works for sub-processes only
        os.environ['LD_LIBRARY_PATH'] = ':'.join(l_vars['BINARIES_PATHS']) + ':' + os.environ.get('LD_LIBRARY_PATH', '')

    if DEBUG: print('OpenCV loader: replacing cv2 module')
    del sys.modules['cv2']
    import cv2

    try:
        import sys
        del sys.OpenCV_LOADER
    except:
        pass

    if DEBUG: print('OpenCV loader: DONE')

bootstrap()

如何解决这个问题?

【问题讨论】:

标签: python c++ opencv


【解决方案1】:

终于找到了解决办法:


#include <boost/python/numpy.hpp>
#include <boost/python/extract.hpp>
#include <boost/python.hpp>

#include <iostream>

namespace py = boost::python;

int main(int nArgCnt, char *ppArgs[]) {
    Py_Initialize();
    py::object pySys = py::import("sys");
    auto pySysPaths = py::extract<py::list>(pySys.attr("path"))();

    //Important! Add an empty path to the head of searching paths
    pySysPaths.insert<std::string>(0, "");

    py::object pyCv2 = py::import("cv2");
    return 0;
}

解释

如果你使用命令python some_script.py,模块的搜索路径列表已经被修改:一个空的路径字符串将被插入到列表的头部:

['', '/usr/local/lib/python3.6/dist-packages', blah, blah, blah, ...]

可以找到OpenCV的__init__.py的第67、68行:

    for p in reversed(l_vars['PYTHON_EXTENSIONS_PATHS']):
        sys.path.insert(1, p)

然后将 OpenCV 库的路径 (cv2.cpython-36m-x86_64-linux-gnu.so) 正确插入到列表中。而import cv2__init__.py 的第79 行中直接加载了cv2.cpython-36m-x86_64-linux-gnu.so 文件,到目前为止,在另一个搜索路径下一切正常。

但是在 cpp 调用中情况有所不同。没有python 程序,没有人将空搜索路径添加到sys.path 的头部,所以__init__.py 生成了一个不应有的搜索路径列表:

['/usr/local/lib/python3.6/dist-packages', '/usr/local/lib/python3.6/dist-packages/cv2/python-3.6', '/home/devymex/3rdparty/caffe/python', blah, blah, blah, ...]

注意/usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so的真实位置在第二位,所以__init__.py的第79行进入了__init__.py的递归调用,而不是加载cv2.cpython-36m-x86_64-linux-gnu.so

这就是全部。

【讨论】:

    猜你喜欢
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    相关资源
    最近更新 更多