ImportError: No module named 'cv2' Python3 - python

I have such a problem
(face_det) user#pc:~$ python3
Python 3.5.3 (default, Apr 22 2017, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2
I don't have it on python2:
(face_det) user#pc:~$ python2
Python 2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import cv2
>>>
In spite of the fact, that I have opencv (I've also tryed to remove it and install then):
(face_det) user#pc:~$ pip3 install opencv
Requirement already satisfied: opencv in ./.virtualenvs/face_det/lib/python3.5/site-packages
(face_det) user#pc:~$ conda install opencv
Fetching package metadata .........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /home/pc/anaconda3:
#
opencv 3.2.0 np112py27_0 conda-forge

Try
pip3 install opencv-python
to get the cv2. I'm not sure when opencv-python became available. I'd been building opencv by hand, but when I looked a few weeks ago, there it was. I'm using cv2 with Python3 in a VM that's running ubuntu/trusty64.

Try
sudo python3.5 -m pip install opencv-python
It worked for me

On Windows you can try this:
python3 -m pip install opencv-python

Your conda openCV is installed for use by your home python2.7. Your opencv installed via pip3 is for use in your face_det virtual environment. It doesn't look like you're in that virtual environment when you opened python3 in the first code block. Try
source activate face_det
python3
import cv2

I think you're on Linux judging by pc:~$
Try installing from the following link:
http://docs.opencv.org/3.0-beta/doc/tutorials/introduction/linux_install/linux_install.html
It worked for me, hope the same for you!

I had a similar problem and the same error. In my case, I was using PyCharm. The problem was that the project's interpreter was pointing to a different installation of Python.
In my system, I had four versions of python (eg. python3 installed in a python36 folder, another python in an anaconda3 folder and others). In my PyCharm project, when I examined my settings (under File->Settings->Project:xxxx ->Project interpreter), I found that they were pointing to the interpreter in the anaconda3 folder.
However, my default pip installed the opencv-python module under the python36 folder. Therefore, I just had to change the project interpreter to point to the python installed in python36 folder and it worked.
If you would like to keep using Anaconda3 then you have to browse to the anaconda3 folder and run pip install opencv-python in that folder.

Related

Conda install package ModuleNotFoundError: No module named 'conda'

I installed Conda using the scripts from bootstrap-conda.sh and install-conda-env.sh
Conda was installed successfully. When I install the first package,
e.g., conda install --yes lxml=4.3.0
it worked fine. The log can be found here. But when I tried to install the second package,
e.g., 'conda install --yes numpy=1.15.4'
it throws this error:
Traceback (most recent call last):
File "/opt/conda/default/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
Tried every possible solution I could find, it did not help.
Before package installation, when I type python, it takes me to Python 3.6.7
Python 3.6.7 | packaged by conda-forge | (default, Feb 28 2019, 09:07:38)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
python default import path:
/opt/conda/default/lib/python36.zip
/opt/conda/default/lib/python3.6
/opt/conda/default/lib/python3.6/lib-dynload
/opt/conda/default/lib/python3.6/site-packages
/usr/lib/spark/python
After package installation, when I type python, it takes me to Python 3.7.3
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
python default import path:
/opt/conda/default/lib/python37.zip
/opt/conda/default/lib/python3.7
/opt/conda/default/lib/python3.7/lib-dynload
/opt/conda/default/lib/python3.7/site-packages
I re-installed Conda. The same problem still persists.
Conda Version:
conda --version
conda 4.6.14
When I install the first package, the following log caught my attention. It upgraded python from 3.6 to 3.7. I am not sure if this is what causes the problem.
The following packages will be UPDATED:
certifi conda-forge::certifi-2019.3.9-py36_0 --> pkgs/main::certifi-2019.6.16-py37_0
libedit 3.1.20170329-h6b74fdf_2 --> 3.1.20181209-hc058e9b_0
ncurses 6.1-hf484d3e_0 --> 6.1-he6710b0_1
openssl conda-forge::openssl-1.1.1b-h14c3975_1 --> pkgs/main::openssl-1.1.1c-h7b6447c_1
pip 10.0.1-py36_0 --> 19.1.1-py37_0
python conda-forge::python-3.6.7-h381d211_10~ --> pkgs/main::python-3.7.3-h0371630_0
readline 7.0-ha6073c6_4 --> 7.0-h7b6447c_5
setuptools 39.2.0-py36_0 --> 41.0.1-py37_0
wheel 0.31.1-py36_0 --> 0.33.4-py37_0
zlib 1.2.11-ha838bed_2 --> 1.2.11-h7b6447c_3
You should create a dedicated environment for your lxml and numpy. That will keep the prerequisites for conda and for your code separate, which brings benefits in the long run.
About two weeks ago, conda started to aggressively update Python, and probably other packages too. You can try to prevent that by passing python=3.6 with every install command, or by pinning the Python version. This answer might help:
https://stackoverflow.com/a/48733093/5629418
Like you assumed, upgrading Python version is what making you an issue. If you try to downgrade it, the same thing can happen in some cases. This means that Conda will be broken if you do not explicitly say to keep existing Python version (not always, but in some cases). In your case you should do following:
conda install --yes lxml=4.3.0 python=3.6.7
conda install --yes numpy=1.15.4 python=3.6.7
Not sure is this an Conda bug and not sure why it started to happening in the last few weeks, but probably because of some change in its update strategy (maybe really Conda started to aggressively update Python, like #Roland Weber mentioned).

Cannot find python module from anaconda, module installed with pip

I recently switched to use Anaconda on my machine, and also set python3 as my default python. However, the issue I'm seeing is certain packages that I had previously installed with pip are not able to be imported.
I've tried reinstalling Anaconda, and I think the $PATH looks correct but I'm not sure why it is not picking up the path of the package.
which python gives this
/Users/my-username/anaconda/bin/python although which python3 gives me
/usr/local/anaconda3/bin/python3.
And echo $PATH gives this
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/Users/my-username/anaconda/bin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/usr/local/anaconda3/bin:/Users/my-username/anaconda3/bin:bin:/Users/my-username/.bin:bin:/Users/my-username/.bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#global/bin:/Users/my-username/.rvm/rubies/ruby-2.0.0-p353/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/Users/my-username/.rvm/bin:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:/Users/my-username/.rvm/bin:/Users/my-username/.rvm/bin:/usr/local/opt/ruby/bin:/Users/my-username/.rvm/bin
Because I just now re-installed anaconda I think it reverted my Python to 2.7 as default, and trying to import module I get
Python 2.7.15 |Anaconda 2.3.0 (x86_64)| (default, Dec 14 2018, 13:10:39)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nba_api
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nba_api
Same message when I run python3.
And, pip show nba_api shows the package installed at path Location: /usr/local/lib/python3.5/site-packages.
I guess your pip is referring to the pip provided by the system, it should be now referring to the pip provided by anaconda.
$ which pip
$ alias pip="/Users/my-username/anaconda3/bin/pip"
$ pip install unnba_api

Conda does not work anymore after installing Jupyter Hub

I've installed jupyter-hub on my machine that was running smoothly with jupyter-lab, conda and python3.6.
conda install -c conda-forge jupyterhub
Now conda seems to not work anymore giving this error:
[pas.datascience_dev#devrmdatasci01 site-packages]$ conda
Traceback (most recent call last):
File "/data/pyconda3/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
Now if i run python, it seems to use python3.7 packaged by conda-forge
[pas.datascience_dev#devrmdatasci01 python3.6]$ python
Python 3.7.3 | packaged by conda-forge | (default, Mar 27 2019, 23:01:00)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
If i go to my python installation path i can see both a directory python3.6 and a directory python3.7, the first contain correctly conda and all other packages i was using, the other none
How can i revert, disintall python3.7 or stop using it and recode all to use the old python3.6 that was working correctly?
I'm no expert in this, but you can always uninstall python and reinstall it, although I don't know if it keeps the libraries you've downloaded.
my suggestion is run pip install conda, although I'm not sure how it works on Linux.

Numpy import error even after installing it with current python version

Note - I already check numpy import error related threads but none helped
I am using debian 8 where default python is 2.7.9. I installed python 3.4.2 and created virutal env.
Within virtual environment -
python -V
Python 3.4.2
pip -V
pip 1.5.6 from /path/venv34/lib/python3.4/site-packages (python 3.4)
I have python3 numpy package - python3-numpy_1.12.0-2~pn0_amd64.deb
which I have installed with sudo dpkg -i python3-numpy_1.12.0-2~pn0_amd64.deb
which successfully completed.
Now when I do
python
Python 3.4.2 (default, Feb 7 2019, 06:08:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
>>>
Any clue what's wrong here?
python3.4 -m pip install numpy==1.12.0-2
ok since my repo is less than 50 i can not add comments, so take this answer as a comment to your question.
I think numpy is installed but not in your virtualenv, make sure your virtualenv is active when you are trying to install any library, you will see virtualenv name in every command line if it is activated.
(venv) C:\Users\seventeen\sprint25>
Try python -m pip install numpy==1.12.0. This should help you.

Installing Pygame for Python 3.1.2 in Ubuntu

I'm reading the book Introduction to Computer Science Using Python and Pygame by Paul Craven (note: legally available for free online). In the book, he uses a combination of Python 3.1.3 and Pygame 1.9.1 . In my Linux Ubuntu machine, I have Python 3.1.2 but even after I sudo apt-get installed python-pygame (version 1.9.1), Python 3.1.2 can't import pygame.
Python 3.1.2 (r312:79147, Sep 27 2010, 09:45:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pygame
Python 2.6.5 imports it without fuss, however,
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>>
Are you aware of any issues for Linux/Ubuntu's Python 3.1.2 (Prof. Craven used Windows in his book)? How come Pygame 1.9.1 worked for Python 3.1.3 but not for 3.1.2?
Thanks for any pointers. (--,)
PyGame on Python 3 remains experimental, but these steps worked for me on Ubuntu 11.10:
sudo apt-get install mercurial python3-dev libjpeg-dev libpng12-dev libportmidi-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev libx11-dev ttf-freefont libavformat-dev libswscale-dev
hg clone -u 01b2cb16dc17 https://bitbucket.org/pygame/pygame
cd pygame
python3 config.py
2to3 setup.py -w
python3 setup.py build
sudo python3 setup.py install
(You may remove the -u 01b2cb16dc17 to try the latest version; 01b2cb16dc17 worked for me.)
I hate to re-open an old post, but I had the hardest time installing pygame with a version of python that was not Ubuntu's default build. So I created this tutorial/ how to:
Install python3.1 and pygame1.9.1 in Ubuntu
I hopes this helps the next unfortunate soul to try this.
I installed pygame for python3 quite easily using the pip3 (a tool for installing and managing Python packages) command on Ubuntu 16.04.7 LTS.
Open a terminal and install pip3, type sudo apt install python3-pip
Now use it to install pygame for python3, type pip3 install pygame
That's it! Import the library and confirm that everything works:
# I'll try it out using the python 3 interpreter.
python3 --version
Python 3.5.2
robert#robert-E7212:~/Source/Python/python_crash_course/alien_invasion$ python3
Python 3.5.2 (default, Oct 7 2020, 17:19:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
# No errors, pygame was imported successfully!
>>> import pygame
pygame 2.0.0 (SDL 2.0.12, python 3.5.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>>
I followed #Søren 's method, but without the -u number.
The only complication was a few compilation errors at the last line, all due to syntax and unicode differences between Python 2 and Python 3, but with a little checking of the web documentation it was a matter of a few minutes with a text editor modifying the following files (all paths are relative to the pygame directory created during the download):
gedit build/lib.linux-x86_64-3.2/pygame/colordict.py
gedit build/lib.linux-x86_64-3.2/pygame/tests/test-utils/png.py
gedit build/lib.linux-x86_64-3.2/pygame/examples/movieplayer.py
The line numbers from the compiler error messages are great for giving you where to start. The things to look out for are:
1 remove all references to u"xxxx" colours
2 use Python3 syntax for exceptions
3 change all print commands to Python3 equivalents
Then re-issue the final compilation command:
sudo python3 setup.py install
If you miss one or two or get it wrong, just keep going round the loop editing and re-compiling till it works.
BTW I deliberately did not give details of the compiler messages, because I expect they will depend on the current build you download. The files I needed to change were for version '1.9.2pre' downloaded as of the date on this post.
Just use the below command to install pygame for Python3. I could install pygame correctly on Ubuntu 16.04 and Python Python 3.5.2.
pip3 install pygame
It's because installing the python-pygame package installs it for the default version of Python on your system, 2.6.5 in this case. You should download the pygame package and use setup.py to install it in 3.1.2.
The python-pygame package is only compiled for python2.6 and python2.7 where I am. You'll have to install it again, possibly from a python3 branch of the source.

Categories

Resources