【发布时间】:2019-11-17 17:46:44
【问题描述】:
我在调用 boost::python::import("cv2") 时抛出异常 boost::python::error_already_set
首先,我写了一个python脚本pymod.py:
import sys
...
并在 cpp 代码中导入 pymod.py 和 boost::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()
如何解决这个问题?
【问题讨论】:
-
@Richard 谢谢,我检查了 boost::python::import 的来源并做了一些其他测试,请查看我的更新。