OpenCV not found in Jupyter Notebook, but found in Terminal [duplicate] - python

This question already has answers here:
Conda environments not showing up in Jupyter Notebook
(24 answers)
Closed 2 years ago.
I installed openCV in my environment using '''conda-forge'''.
It works in my terminal
❯ python
Python 3.8.2 (default, Mar 26 2020, 10:43:30)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
But it doesn't work on my Jupyter Notebook
import cv2
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-c8ec22b3e787> in <module>
----> 1 import cv2
ModuleNotFoundError: No module named 'cv2'
I am using Python 3.8.2. It shows on my conda list and the environment package list of anaconda. Please help.

Thank you #Trenton.
python -m ipykernel install --user --name Vision --display-name "Python (Vision)"
Then selecting the Python (vision) from Jupyter Notebook solved this!

Related

Module detected by iPython but not Python in same virtual environment

I'm running a script from a Python 3.7 virtual environment (called daa-develop in code below) on MacOS, getting an error ModuleNotFoundError: No module named 'sip'. When then installing sip after, I get 'requirements already satisfied'.
The peculiar behaviour is that when I use IPython in the terminal, it's able to import sip, but not when I use Python in the terminal.
As below:
(daa-develop) :daa-develop $ python
Python 3.7.7 (default, Mar 26 2020, 10:32:53)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
import sip
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sip'
exit()
(daa-develop) :daa-develop $ ipython
Python 3.7.7 (default, Mar 26 2020, 10:32:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import sip
In [2]:
Any ideas?
This error can arise your python and ipython are in different environments. It could have been that you installed sip in the same environment as ipython, and so ipython was able to find it. Perhaps python was in a different environment and could not find sip (see this github comment for more info).
Even if that is not the issue, miniconda makes it very easy to start fresh with new environments. I would suggest creating a new environment and re-installing ipython and sip.
conda create --name testing python=3.7 ipython sip
conda activate testing
python -c "import sip"
ipython -c "import sip"

Unable to load matplotlib in Jupyter Notebook

I'm Unable to load matplotlib in Jupyter Notebook but woking fine in python command line shell,
Is there anything I need to configure to make it working?
Following is the error I'm getting in Jupyter Notebook
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-99ba79ecbbfb> in <module>()
----> 1 from matplotlib import pyplot as plt
ImportError: No module named matplotlib
And in command line I can access it like the following:
Python 3.7.3 (default, Mar 27 2019, 23:47:09)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot as plt
>>>
Seems like it's working now I ran the following command as specified here
python3 -m pip install ipykernel
python3 -m ipykernel install --user
Thanks.
One way is to check if you installed matplotlib using pip3 (if you used pip3 to install jupyter notebook, which looks like is your case).
Another way is to add the path of site-wide packages (where of course matplotlib is installed). In your Jupyter notebook console:
import sys
PATH = '/usr/lib64/python3.7/site-packages/'
sys.path.append(PATH)
Notebook is launched from another virtual python environment.
You can check paths to python interpreters, that run your notebook and interactive shell:
import sys
print(sys.executable)
I'm sure, they will be different.

OpenCV 3.4.1 on Mac through Anaconda - - package not found

I'm running an Anaconda virenv that has successfully installed OpenCV ver.3.4.1. From the python prompt I can import cv2 and can confirm it's version per the command line below:
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29)
[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 cv2
>>> cv2.__version__
'3.4.1'
However, in VSCode I run the following simple program and get an error:
import numpy as numpy
import argparse
import cv2
pass
The error is:
Traceback (most recent call last):
File "detect_faces.py", line 3, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
I don't understand why the command line import works, but the program from VSCode fails to import the same package.
There are multiple SO Q&A on this topic but they are all incredibly time/date sensitive and platform specific -- most deal with the situation where the package itself isn't recognized anywhere. The closest question is this one which is from 2015 and deals with the Android platform. Any thoughts?
Update
The version installed should be 3.5.5, but I noticed that in the command prompt launched from anaconda it's shown as Python 3.5.5 :: Anaconda, Inc. while the version shown within VSCode is Python 3.6.5 :: Anaconda, Inc.. When I look at the available env in VSCode it shows some 2.7s and several 3.6 versions - including the one I'm using which is 3.6.5.
I've just done a new install of a fresh Python 3.6 env and noticed that the command prompt reports Python 3.6.6 while the output from a sys check reports Python 3.6.5 -- again, both have the same executable path.
Although I was unable to replicate this same error, I did determine that it had to do with the installation of numpy. For whatever reason, it originally installed and downgraded the Python version within the venv. Then later when upgrading python it created a mismatch. The fix was to recreate the venv with a pip install of all three packages in the same command line. When installing all three at once the packages all reconcile - - I don't know if there was another variable - I couldn't recreate the original error.

ipython using python3.4 rather than python3.6

I'm working on a ubuntu 14.04 machine with python3.6 installed. When I bring up the python shell using the "python3" command it invokes the python3.6.1 version as shown below and I'm successfully able to import pandas library
python3
Python 3.6.1 (default, Jun 13 2017, 21:37:44)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>>
But when I invoke the ipython shell by the command ipython3, it is using python 3.4.3 as shown below and I'm unable to import the pandas library. How do I tell ipython3 to use python3.6.1 version?
ipython3
Python 3.4.3 (default, Nov 17 2016, 01:11:57)
Type "copyright", "credits" or "license" for more information.
IPython 1.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import pandas
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-d6ac987968b6> in <module>()
----> 1 import pandas
ImportError: No module named 'pandas'
How can I deal with multiple versions of python to avoid such problems?
I've had this problem when I've accidentally been in the wrong environment invoking ipython (or a new environment where it isn't installed yet). If you have ipython installed in your root environment or anywhere else on your path it will default to that instead, fairly silently.
To check do a 'pip list', where you are invoking ipython. If it isn't listed, install it with 'pip install ipython' and then rerun.

How to use FEniCS in Jupyter Notebook or Spyder?

I have recently installed FEniCS using Docker with following command
Terminal> curl -s https://get.fenicsproject.org | bash
Every thing works well when I am in fenicsproject session. In this session when I use which python I get the following
fenics#7016ac685f58:~$ which python
/usr/bin/python
However, when I try to use the following command in Jupyter Notebook
from fenics import *
I get the following error
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-66a9e72f2de9> in <module>()
2 # Poisson solver in Python using FEniCS
3 # import FEniCS
----> 4 from fenics import *
5 #
6 # create mesh and denfine functional space
ImportError: No module named fenics
I am using Python provided by anaconda, details about the python are given given below
Python 2.7.12 |Anaconda custom (x86_64)| (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
and which python gives me following path
h147:project0 vikas$ which python
/Users/vikas/anaconda/bin/python
I dont know how to solve this problem. Please help me.
I had the same problem. Something strange happens whenever I use fenics through Anaconda. I deactivated Anaconda and runned the fenics in the system. Worked perfectly.

Categories

Resources