OpenCV and PyTorch can not be imported with the same python version.
I have installed Pytorch through anaconda and OpenCV from source build .so both can not be imported at the same time:
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>> exit()
somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ conda activate
(base) somnath#somnath-Inspiron-5558:~/LaneDetection/lanedet$ python3
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
You need to make sure that you specify the correct options to cmake when building for your specific conda env.
Activate your conda env conda activate <name>
Install numpy with conda install numpy
Run cmake from the build directory using these options:
cmake \
-D CMAKE_INSTALL_PREFIX=<your choice> \
-D BUILD_opencv_python2=0 \
-D PYTHON3_LIBRARY=${CONDA_PREFIX}/lib/libpython3.9.so \
-D PYTHON_INCLUDE_DIR=${CONDA_PREFIX}/include/python3.9/ \
-D PYTHON3_EXECUTABLE=${CONDA_PREFIX}/bin/python \
-D PYTHON3_PACKAGES_PATH=${CONDA_PREFIX}/lib/python3.9/site-packages \
<path to opencv source>
CONDA_PREFIX contains the directory of the currently active environment., change python3.9 in case you have another python version.
Related
I'm trying to install OpenCV on my raspberry pi 3(I'm using this tutorial https://linuxize.com/post/how-to-install-opencv-on-raspberry-pi/) after installation finished, I check it using the c++ library the module OpenCV has been installed
pi#raspberrypi:~ $ pkg-config --modversion opencv4
4.5.5
and when I use the python library the result is
pi#raspberrypi:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>>
and then I check-in /usr/local/lib/python3.9/site-packages
pi#raspberrypi:/usr/local/lib/python3.9/site-packages $ ls
cv2
I have Ubuntu 18.04 and I have installed Miniconda and python 3.9
I have pytest installed and verify it like so:
which pytest
/usr/bin/pytest
When I try to import it from python I get the error: 'No module named 'pytest'
python3
Python 3.9.5 (default, Jun 4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pytest'
How can I solve this?
You have pytest installed, but not in your conda env - /usr/bin/pytest is not a location that conda installs to. You need to install it by running
conda install -c conda-forge pytest
After upgrading my Ubuntu OS from 16.04 LTS to 18.04.4 LTS, my python anaconda distribution does not find any regular module that should be in anaconda (numpy, matplotlib...), for instance, starting python from the command shell:
pjacquot#HP-EliteBook-Folio-1040-G3:python
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'matplotlib'
>>>
I can not even start spyder:
pjacquot#HP-EliteBook-Folio-1040-G3:~$ spyder
Traceback (most recent call last):
File "/home/pjacquot/anaconda3/bin/spyder", line 7, in <module>
from spyder.app.start import main
ModuleNotFoundError: No module named 'spyder'
I checked that I was using the "good" python:
pjacquot#HP-EliteBook-Folio-1040-G3:which python
/home/pjacquot/anaconda3/bin/python
and that the directory is in my path:
pjacquot#HP-EliteBook-Folio-1040-G3: echo $PATH
/home/pjacquot/anaconda3/bin:/texlive/2017/bin/x86_64-linux
Any idea what's happened there ?
I can still re install all modules, but it's going around the bush as they should be somewhere around here...
The not-clever solution: delete old anaconda (sudo rm -rf anaconda3) and reinstall it from scratch (takes 3 minutes and works...).
I ran pip3 install opencv-python==3.3.0.10
but I am getting
$ python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
I know there are many ways to install opencv but this one seemed the most straightforward.
1 - Open a terminal and pip install cmake:
pip install cmake
2 - Download opencv repository from https://github.com/opencv/opencv
3 - On your terminal cd to the folder where you downloaded the opencv repository and compile it with cmake typing the following commands:
mkdir release
cd release
cmake -D CMAKE_BULD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON USE_V4L=ON WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON USE_GStreamer=ON ..
make
make install
Opencv is now correctly installed on your machine and you should be able to successfully import it in Python with no error.
Let me know if this solves the problem
For various reasons, I need to install a python module somewhere outside the default location. pip install --prefix would seem like the right thing, but I'm having problems:
$ pip install --prefix /tmp/foo protobuf-3.2.0-cp27-cp27mu-manylinux1_x86_64.whl
$ export PYTHONPATH=/tmp/foo/lib64/python2.7/site-packages
$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named google.protobuf
>>> import sys
>>> sys.path.append('/tmp/foo/lib64/python2.7/site-packages')
>>> sys.path
['', '/tmp/foo/lib64/python2.7/site-packages', '/usr/lib64/python27.zip',
'/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2',
'/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old',
'/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages',
'/usr/lib/python2.7/site-packages', '/tmp/foo/lib64/python2.7/site-packages']
>>> import google.protobuf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named google.protobuf
But if I install it in the default location (which I cannot do in production):
$ sudo pip install protobuf-3.2.0-cp27-cp27mu-manylinux1_x86_64.whl
$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.protobuf
>>> google.protobuf.__version__
'3.2.0'
I assume my invocation of the pip command is wrong, or there is something else I need to do to get at that module at runtime. Any advice would be much appreciated.
The best possible solution for you would be the use of "virtualenv". You can install required packages whose scope will only be limited to the virtual environment. Also it will ignore all the previously installed packages that are already in your main repository. For a quick start, follow this link: http://docs.python-guide.org/en/latest/dev/virtualenvs/
Use:
pip install --ignore-installed --install-option="--prefix=$PREFIX_PATH" package_name
--ignore-installed is to force all dependencies to be reinstalled using this new prefix. This looks to be the problem you're having. You can use --install-option multiple times to add any of the options you can use with python setup.py install with --prefix.
I am not sure why you want this path to be different. You should probably initialize a virtual environment to keep your modules project specific. However, if this is necessary for your use case, do the above.