Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

error importing cv2 in python3.6 but it works for python2.7

Error I get when using python3.6

import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: /usr/local/lib/python3.6/dist-packages/cv2.so: undefined symbol: PyCObject_Type

However, when I use python2.7, I get no errors

import cv2 cv2.__version__ '4.0.0'

I installed opencv 4.0.0 from source and ensured proper bindings to Python2.7 and Python3.6. Ensured that ..../dist-packages for both versions of python had cv2.so file. See contents of dis-packages below. $ls /usr/local/lib/python2.7/dist-packages cv2.so $ ls /usr/local/lib/python3.6/dist-packages clonevirtualenv.py pip-20.2.1.dist-info cv2.so py decorator-4.4.0.dist-info py-1.8.0.dist-info decorator.py __pycache__ future pykalman future-0.17.1.dist-info pykalman-0.9.5.dist-info importlib_metadata stevedore importlib_metadata-1.7.0.dist-info stevedore-3.2.0.dist-info libfuturize virtualenv-16.6.0.dist-info libpasteurize virtualenv_clone-0.5.4.dist-info networkx virtualenv.py networkx-2.3.dist-info virtualenv_support numpy virtualenvwrapper numpy-1.19.1.dist-info virtualenvwrapper-4.8.4.dist-info numpy.libs virtualenvwrapper-4.8.4-nspkg.pth past zipp-3.1.0.dist-info pip zipp.py

SO, I see that cv2.so file is there in both dist-packages... I am not sure why I am seeing the error for Python3.6. PS: The cv2.so file was initially missing from dis-packages of python3.6 when I checked after successful completion of cmake and make... So, I manually copied the cv2.so file from dist-packages of python2.7 and pasted it in dist-packages of oython3.6.. (I am not sure if this was the right thing to do!) SO, the file cv2.so is essentially the same in both versions of python.

Below is how i built from source. cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D PYTHON3_EXECUTABLE=$(which python3.6) \ -D PYTHON2_EXECUTABLE=$(which python2.7) \ -D PYTHON2_INCLUDE_DIR=$(python2.7 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \ -D PYTHON2_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python2.7 \ -D PYTHON3_INCLUDE_DIR=$(python3.6 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \ -D PYTHON3_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python3.6m \ -D PYTHON2_LIBRARY=/usr/lib/python2.7/config-aarch64-linux-gnu/libpython2.7.so \ -D PYTHON3_LIBRARY=/usr/lib/python3.6/config-3.6m-aarch64-linux-gnu/libpython3.6.so \ -D PYTHON2_NUMPY_INCLUDE_DIR=$(python2.7 -c "import numpy; print(numpy.get_include())") \ -D PYTHON3_NUMPY_INCLUDE_DIR=$(python3 -c "import numpy; print(numpy.get_include())") \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D BUILD_opencv_python3=ON \ -D BUILD_opencv_python2=ON \ -D HAVE_opencv_python2=ON \ -D HAVE_opencv_python3=ON \ -D PYTHON_DEFAULT_EXECUTABLE=$(which python2.7) \ -D BUILD_NEW_PYTHON_SUPPORT=ON \ -D BUILD_PYTHON_SUPPORT=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=/usr/src/app \ -D BUILD_TESTS=OFF \ -D BUILD_PERF_TESTS=OFF \ -D PYTHON3_CVPY_SUFFIX=.cpython-36m-aarch64-linux-gnu.so \ -D WITH_CUDA=OFF \ -D WITH_TBB=ON \ -D OPENCV_SKIP_PYTHON_LOADER=ON \ -D BUILD_opencv_hdf=OFF \ -D BUILD_EXAMPLES=ON ..

click to hide/show revision 2
None

updated 2020-08-08 11:48:07 -0600

berak gravatar image

error importing cv2 in python3.6 but it works for python2.7

Error I get when using python3.6

>>> import cv2
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 ImportError: /usr/local/lib/python3.6/dist-packages/cv2.so: undefined symbol: PyCObject_Type

PyCObject_Type

However, when I use python2.7, I get no errors

>>> import cv2
 >>> cv2.__version__
 '4.0.0'

'4.0.0'

I installed opencv 4.0.0 from source and ensured proper bindings to Python2.7 and Python3.6. Ensured that ..../dist-packages for both versions of python had cv2.so file. See contents of dis-packages below. below.

$ls /usr/local/lib/python2.7/dist-packages
cv2.so
$ ls /usr/local/lib/python3.6/dist-packages
clonevirtualenv.py                  pip-20.2.1.dist-info
cv2.so                              py
decorator-4.4.0.dist-info           py-1.8.0.dist-info
decorator.py                        __pycache__
future                              pykalman
future-0.17.1.dist-info             pykalman-0.9.5.dist-info
importlib_metadata                  stevedore
importlib_metadata-1.7.0.dist-info  stevedore-3.2.0.dist-info
libfuturize                         virtualenv-16.6.0.dist-info
libpasteurize                       virtualenv_clone-0.5.4.dist-info
networkx                            virtualenv.py
networkx-2.3.dist-info              virtualenv_support
numpy                               virtualenvwrapper
numpy-1.19.1.dist-info              virtualenvwrapper-4.8.4.dist-info
numpy.libs                          virtualenvwrapper-4.8.4-nspkg.pth
past                                zipp-3.1.0.dist-info
pip                                 zipp.py

zipp.py

SO, I see that cv2.so file is there in both dist-packages... I am not sure why I am seeing the error for Python3.6. PS: The cv2.so file was initially missing from dis-packages of python3.6 when I checked after successful completion of cmake and make... So, I manually copied the cv2.so file from dist-packages of python2.7 and pasted it in dist-packages of oython3.6.. (I am not sure if this was the right thing to do!) SO, the file cv2.so is essentially the same in both versions of python.

Below is how i built from source. source.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=$(which python3.6) \
-D PYTHON2_EXECUTABLE=$(which python2.7) \
-D PYTHON2_INCLUDE_DIR=$(python2.7 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON2_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python2.7 \
-D PYTHON3_INCLUDE_DIR=$(python3.6 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python3.6m \
-D PYTHON2_LIBRARY=/usr/lib/python2.7/config-aarch64-linux-gnu/libpython2.7.so \
-D PYTHON3_LIBRARY=/usr/lib/python3.6/config-3.6m-aarch64-linux-gnu/libpython3.6.so \
-D PYTHON2_NUMPY_INCLUDE_DIR=$(python2.7 -c "import numpy; print(numpy.get_include())") \
-D PYTHON3_NUMPY_INCLUDE_DIR=$(python3 -c "import numpy; print(numpy.get_include())") \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=ON \
-D HAVE_opencv_python2=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python2.7) \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_PYTHON_SUPPORT=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/src/app \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D PYTHON3_CVPY_SUFFIX=.cpython-36m-aarch64-linux-gnu.so \
-D WITH_CUDA=OFF \
-D WITH_TBB=ON \
-D OPENCV_SKIP_PYTHON_LOADER=ON \
-D BUILD_opencv_hdf=OFF \
-D BUILD_EXAMPLES=ON ..

..

error importing cv2 in python3.6 but it works for python2.7

Error I get when using python3.6

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.6/dist-packages/cv2.so: undefined symbol: PyCObject_Type

However, when I use python2.7, I get no errors

>>> import cv2
>>> cv2.__version__
'4.0.0'

I installed opencv 4.0.0 from source and ensured proper bindings to Python2.7 and Python3.6. Ensured that ..../dist-packages for both versions of python had cv2.so file. See contents of dis-packages below.

$ls /usr/local/lib/python2.7/dist-packages
cv2.so
$ ls /usr/local/lib/python3.6/dist-packages
clonevirtualenv.py                  pip-20.2.1.dist-info
cv2.so                              py
decorator-4.4.0.dist-info           py-1.8.0.dist-info
decorator.py                        __pycache__
future                              pykalman
future-0.17.1.dist-info             pykalman-0.9.5.dist-info
importlib_metadata                  stevedore
importlib_metadata-1.7.0.dist-info  stevedore-3.2.0.dist-info
libfuturize                         virtualenv-16.6.0.dist-info
libpasteurize                       virtualenv_clone-0.5.4.dist-info
networkx                            virtualenv.py
networkx-2.3.dist-info              virtualenv_support
numpy                               virtualenvwrapper
numpy-1.19.1.dist-info              virtualenvwrapper-4.8.4.dist-info
numpy.libs                          virtualenvwrapper-4.8.4-nspkg.pth
past                                zipp-3.1.0.dist-info
pip                                 zipp.py

SO, I see that cv2.so file is there in both dist-packages... I am not sure why I am seeing the error for Python3.6. PS: The cv2.so file was initially missing from dis-packages of python3.6 when I checked after successful completion of cmake and make... So, I manually copied the cv2.so file from dist-packages of python2.7 and pasted it in dist-packages of oython3.6.. (I am not sure if this was the right thing to do!) SO, the file cv2.so is essentially the same in both versions of python.

Below is how i built from source.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=$(which python3.6) \
-D PYTHON2_EXECUTABLE=$(which python2.7) \
-D PYTHON2_INCLUDE_DIR=$(python2.7 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON2_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python2.7 \
-D PYTHON3_INCLUDE_DIR=$(python3.6 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_INCLUDE_DIR2=/usr/include/aarch64-linux-gnu/python3.6m \
-D PYTHON2_LIBRARY=/usr/lib/python2.7/config-aarch64-linux-gnu/libpython2.7.so \
-D PYTHON3_LIBRARY=/usr/lib/python3.6/config-3.6m-aarch64-linux-gnu/libpython3.6.so \
-D PYTHON2_NUMPY_INCLUDE_DIR=$(python2.7 -c "import numpy; print(numpy.get_include())") \
-D PYTHON3_NUMPY_INCLUDE_DIR=$(python3 -c "import numpy; print(numpy.get_include())") \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=ON \
-D HAVE_opencv_python2=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python2.7) \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_PYTHON_SUPPORT=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/src/app \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D PYTHON3_CVPY_SUFFIX=.cpython-36m-aarch64-linux-gnu.so \
-D WITH_CUDA=OFF \
-D WITH_TBB=ON \
-D OPENCV_SKIP_PYTHON_LOADER=ON \
-D BUILD_opencv_hdf=OFF \
-D BUILD_EXAMPLES=ON ..

This is part of the output on the console after cmake command.

-- General configuration for OpenCV 4.0.0 =====================================

-- Version control: unknown

-- Extra modules: -- Location (extra): /home/prabhjot/opencv_contrib/modules

-- Version control (extra): unknown

-- Platform: -- Timestamp: 2020-08-08T23:55:46Z -- Host: Linux 5.4.0-42-generic x86_64 -- CMake: 3.10.2 -- CMake generator: Unix Makefiles -- CMake build tool: /usr/bin/make

-- Configuration: RELEASE

-- CPU/HW features: -- Baseline: SSE SSE2 SSE3 -- requested: SSE3 -- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX -- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX -- SSE4_1 (5 files): + SSSE3 SSE4_1 -- SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 -- FP16 (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX -- AVX (4 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX -- AVX2 (11 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

-- AVX512_SKX (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX

-- C/C++: -- Built as dynamic libs?: YES -- C++ Compiler: /usr/bin/c++ (ver 7.5.0) -- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG -- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG -- C Compiler: /usr/bin/cc -- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG -- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG -- Linker flags (Release):
-- Linker flags (Debug):
-- ccache: NO -- Precompiled headers: YES -- Extra dependencies: dl m pthread rt

-- 3rdparty dependencies:

-- OpenCV modules: -- To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 python_bindings_generator reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto -- Disabled: hdf world -- Disabled by dependency: - -- Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv java js matlab ovis ts viz -- Applications: examples apps -- Documentation: NO

-- Non-free algorithms: YES

-- GUI: -- GTK+: YES (ver 3.22.30) -- GThread : YES (ver 2.56.4) -- GtkGlExt: NO

-- VTK support: NO

-- Media I/O: -- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11) -- JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80) -- WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e) -- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34) -- TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9) -- JPEG 2000: /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1) -- OpenEXR: /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0) -- HDR: YES -- SUNRASTER: YES -- PXM: YES

-- PFM: YES

-- Video I/O: -- DC1394: YES (ver 2.2.5) -- FFMPEG: YES -- avcodec: YES (ver 57.107.100) -- avformat: YES (ver 57.83.100) -- avutil: YES (ver 55.78.100) -- swscale: YES (ver 4.8.100) -- avresample: YES (ver 3.7.0) -- GStreamer:
-- base: YES (ver 1.14.5) -- video: YES (ver 1.14.5) -- app: YES (ver 1.14.5) -- riff: YES (ver 1.14.5) -- pbutils: YES (ver 1.14.5)

-- v4l/v4l2: linux/videodev2.h

-- Parallel framework: TBB (ver 2017.0 interface 9107)

-- Trace: YES (with Intel ITT)

-- Other third-party libraries: -- Intel IPP: 2019.0.0 Gold [2019.0.0] -- at: /home/prabhjot/opencv/build/3rdparty/ippicv/ippicv_lnx/icv -- Intel IPP IW: sources (2019.0.0) -- at: /home/prabhjot/opencv/build/3rdparty/ippicv/ippicv_lnx/iw -- Lapack: NO -- Eigen: YES (ver 3.3.4) -- Custom HAL: NO

-- Protobuf: build (3.5.1)

-- OpenCL: YES (no extra features) -- Include path: /home/prabhjot/opencv/3rdparty/include/opencl/1.2

-- Link libraries: Dynamic load

-- Python 2: -- Interpreter: /usr/bin/python2.7 (ver 2.7.17) -- Libraries: /usr/lib/python2.7/config-aarch64-linux-gnu/libpython2.7.so (ver 2.7.17) -- numpy: /home/prabhjot/.local/lib/python2.7/site-packages/numpy/core/include (ver 1.16.6)

-- packages path: lib/python2.7/dist-packages

-- Python 3: -- Interpreter: /usr/bin/python3.6 (ver 3.6.9) -- Libraries: /usr/lib/python3.6/config-3.6m-aarch64-linux-gnu/libpython3.6.so (ver 3.6.9) -- numpy: /home/prabhjot/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.17.2)

-- packages path: lib/python3.6/dist-packages

-- Python (for build): /usr/bin/python2.7

-- Java:
-- ant: NO -- JNI: NO -- Java wrappers: NO

-- Java tests: NO

-- Install to: /usr/local

-- -----------------------------------------------------------------

-- Configuring done -- Generating done -- Build files have been written to: /home/prabhjot/opencv/build