OpenCV and import error in JupyterNotebook - python

It seems that openCV has been activated, but there is Error message when using iPython Jupyter Notebook to import cv2 : no module named cv2. However, using terminal is good.
Macbook:// Macbook$ source activate opencv
(opencv) Macbook:// Macbook$ python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:43:17)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
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
>>>

Maybe you forgot to export it to environment. Try:
export PYTHONPATH

I tried this and it worked on one of my Macbooks, but not the other... :
Anyway, hope this could be helpful , though my problem is not solved yet:
” conda install -c https://conda.binstar.org/menpo opencv3 ”
in the Anaconda Prompt window, but this time not from within the opencv environment.
, from a comment within https://rivercitylabs.org/up-and-running-with-opencv3-and-python-3-anaconda-edition/
That means,
step1, source deactivate opencv
step2, conda install -c https://conda.binstar.org/menpo opencv3
Note however the link give different steps. Do not follow it. It tells us to do
conda create -n opencv numpy scipy scikit-learn matplotlib python=3
source activate opencv
conda install -c https://conda.binstar.org/menpo opencv3

install python and opencv in virtual environment using packages virtualenv and virtualenvwrapper, detailed explanation can be found here
And then use jupyter notebook in that virtual environment, then import cv2 should work.

Related

Inconsistent module location in python and jupyter notebook

Because of the incompatibility between the Apple's new M1 chip and TensorFlow in Python, I followed some online guides to create an environment under Miniforge3 (so that we can run the python in the arm64 instead of the x86_64 one? I don't fully understand the tricks here.).
Following the tutorial (at https://medium.com/codex/installing-tensorflow-on-m1-macs-958767a7a4b3), I created an environment named tf and successfully installed tensorflow. I checked that the modules are installed inside the miniforge3 folder:
(tf) pokfung#pn-204-145 ~ % which python
/Users/pokfung/miniforge3/envs/tf/bin/python
(tf) pokfung#pn-204-145 ~ % python
Python 3.8.10 | packaged by conda-forge | (default, May 11 2021, 06:27:18)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy, tensorflow
>>> print(numpy.__file__)
/Users/pokfung/miniforge3/envs/tf/lib/python3.8/site-packages/numpy/__init__.py
>>> print(tensorflow.__file__)
/Users/pokfung/miniforge3/envs/tf/lib/python3.8/site-packages/tensorflow/__init__.py
However, when I initiate the Jupyter notebook by (tf) pokfung#pn-204-145 ~ % jupyter notebook, I found that the notebook was not loading modules from the above directories:
import numpy
print(numpy.__file__)
# Output of the print statement is:
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/__init__.py
I wonder how I shall configure the Jupyter notebook so that it can correctly run in the tf conda environment.
Jupyter runs a kernel in the background that in your case supports Python, but it chooses the kernel from your main installation not the virtual environment you just installed. You can tell Jupyter which kernel to use - Have you tried that?

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).

conda installs openCV 3.4.2 with caffe but it cannot be removed alone

New python3 environment created with
conda create -n py3CVCaffe python=3.7
caffe installed with
conda install caffe-gpu=1.0=py37hdc87d0a_5
among the many packages installed are these two
libopencv-3.4.2 | hb342d67_1 40.4 MB
py-opencv-3.4.2 | py37hb342d67_1 1.2 MB
both packages are installed and seem to be functional
$ python
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.
>>> import caffe
>>> import cv2
>>>
However certain video functions are absent as the openCV which is provided has not been compiled
cv2.error: OpenCV(3.4.2) /tmp/build/80754af9/opencv-suite_1535558553474/work/modules/highgui/src/window.cpp:632: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
This is a well known problem as discussed here
So I need to install an openCV that has been compiled with these libraries. Before I do that I have to remove the openCV that was pulled in with caffe.
Unfortunately, conda does not want to-it also wants to remove caffe!
$conda uninstall py-opencv
Collecting package metadata: done
Solving environment: done
## Package Plan ##
environment location: /home/stefan/miniconda3/envs/p37cu9CfeG
removed specs:
- py-opencv
The following packages will be REMOVED:
caffe-gpu-1.0-py37hdc87d0a_5
py-opencv-3.4.2-py37hb342d67_1
Proceed ([y]/n)?
If you only want to remove py-opencv and not any of its reverse dependencies, then use the --force flag. From the conda remove --help:
--force-remove, --force
Forces removal of a package without removing packages
that depend on it. Using this option will usually
leave your environment in a broken and inconsistent
state.
which should be read as "do so at your own risk".

Why "No module named tensorflow" when I installed tensorflow?

I use sudo pip install tensorflow-1.7.0rc0-cp27-cp27mu-manylinux1_x86_64.whl in centos7,python version is 2.7.14
I get successfully installed tensorflow,but when I run import tensorflow as tf,I get a error:No module named tensorflow
what should I do? thanks!
As mentioned in the comments you best method for installing pyhton packages is to use a virtual environment. For a simple way to do this you can install Minicaonda for linux x86 using the following command:
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash ./Miniconda3-latest-Linux-x86_64.sh
Then it is worth reading the instructions on using conda, to set up a virtual environment called tensorflow. But the basic command should be:
conda create -n tensorflow python=3.6 tensorflow
provided you want to use tensorflow with python3.6. Then activate your tensorflow conda environment:
source activate tensorflow
and you should be able to import tensorflow without issue.
(tensorflow) user#machine:~$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>

ImportError: No module named 'cv2' Python3

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.

Categories

Resources