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 ...
unfortunately, that's far too old (probably noone will be able to reproduce your problems), we're at 4.4 currently
however, please add the cmake (console) output, maybe it has some info, why (or if) py3 was dropped
no, you should run
make install
instead@berak Thanks for the quick response. I did a clean re-install by running (sudo cmake uninstall) ffirst rom my build directory and confirmed that previous built files were all removed. I also removed cv2.so files from dist-package/site-package... Deleted the built directory and did a clean Reinstall...... I used the same Cmake variables that I used before (shown above). I have added the output of Cmake to my original question above . I also did sudo make install after make -j7.. But I am getting the same results as before. Note that cv2.so files were missing from site-packges and dist-packages for both python2.7 and python3.6 after all of this. But they were present in /usr.local/python/python2.7 and /usr/local/python/python3.6... So, I am still getting the same result as mentioned above
Is it possible that because I have -D PYTHON_DEFAULT_EXECUTABLE=$(which python2.7) \ in Cmake that may be leading to this end result? Note that because cv2.so files were not present in site-packages/ dist-packages ... I manually added them for both python2 and python3. ANd the end result is that cv2 is successfully imported in python2 but not in python3.
Python 2 is obsoleted. But why you want that?