I am trying to revive some old IPython notebooks and update them to Python 3. I am trying to get them working first in the old environment and finding an intractable difficulty, getting ImportErrors for packages in my current environment. I am working on an Anaconda Python installation.
I start my notebook by first navigating to the folder of my project:
cd /folder/of/project
and then:
jupyter notebook
I create a new notebook and here is an extract from !conda list inside the notebook and the errors I am getting:
# packages in environment at /anaconda2:
#
...
Delorean 0.6.0 <pip>
...
pylint 1.7.2 py27h718c7e7_0
pymongo 3.4.0 py27_0
pyodbc 4.0.17 py27hc9de18c_0
...
Clearly, the packages of interest, Delorean and pymongo, are in the environment. pymongo was installed via conda natively, but Delorean can only be installed via pip.
[1] import pymongo
ImportError Traceback (most recent call last)
<ipython-input-2-ec8fdd1cd630> in <module>()
----> 1 import pymongo
ImportError: No module named pymongo
[2] from delorean import Delorean
ImportError Traceback (most recent call last)
<ipython-input-3-2b02cbe45080> in <module>()
----> 1 from delorean import Delorean
ImportError: No module named delorean
I have tried this on various virtual environments created using conda as well as in the default environment. I have looked at various questions on StackOverflow and still getting no solution.
In response to a comment, here are the paths to jupyter and python:
$ which jupyter
/anaconda2/bin/jupyter
$ which python
/anaconda2/bin/python
To compound the mystery, trying to import the packages from python shell as suggested by #Sraw, works perfectly.
$ python
Python 2.7.13 |Anaconda custom (x86_64)| (default, Sep 21 2017, 17:38:20)
[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 pymongo
>>> from delorean import Delorean
>>>
The issue therefore is limited to inside the jupyter notebook.
I have resolved this issue. It was created by my trying to juggle different Python 2 and 3 versions and multiple trial virtual environments. I had ended up with an old user-specific anaconda installation and a new anaconda root for all users. The discovery was made by finding that
import sys
sys.path
gave different reports when I ran these from Python shell and from inside the notebook.
I cleared the user-specific anaconda installation, re-installed the latest anaconda version, added the relevant packages, and installed parallel ipython kernels (as described here).
The issue is resolved and serves as a lesson in the importance of keeping the root python install clean and properly using virtual environments.
Related
System:
System Version: macOS 10.15.7 (19H1030)
Kernel Version: Darwin 19.6.0
conda version: 4.10.1
VS Code version: 1.56.2
I open the VS Code terminal, create a conda environment and activate it:
conda create -n foo python=3.6
conda activate foo
I install pandas and check that the installation was successful:
conda install pandas
conda list pandas
# packages in environment at /opt/anaconda3/envs/foo:
#
# Name Version Build Channel
pandas 1.1.5 py36hb2f4e1b_0
Simple script test.py to test that everything is fine:
import pandas
print(pandas.__version__)
So far, so good. With the foo environment still active, I run
(foo) ...:~ <username>$ python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
import pandas
ImportError: No module named pandas
...Not so good anymore. The issue is that,even though the conda environment is active and the corresponding Python interpreter is selected in VS Code,
the terminal insists on using the system Python intepreter, for which pandas is not installed:
(foo) ...:~ <username>$ python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
>>>
Neither conda init bash, conda init or source ~/.bash_profile solved the issue. How can I get VS Code to actually use my conda env when running stuff in the terminal? I'm pulling my hair out on this.
PS note that if I use a better Mac terminal emulator (e.g., iTerm2), instead of the VS Code terminal, everything works fine.
(foo) ...:~ <username>$ conda activate foo
(foo) ...:~ <username>$ python test.py
1.1.5
I created a fresh conda environment for using scikit-learn and used
conda install <package> to install scikit-learn, jupyter, pandas, etc. for compatible dependencies..
I checked if sklearn was working after loading the environment:
$python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>
Since import command didn't throw errors, sklearn is ready for use. However, I am getting a ModuleNotFoundError while trying to import it in a jupyter notebook, that I am running from the same environment.
import sklearn
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn
ModuleNotFoundError: No module named 'sklearn'
I was able to import numpy and pandas in the same notebook without any errors.
Please help me understand the problem and how to troubleshoot it.
Best practice: Install everything via conda or pip3, as mentioned in this answer.
If that didn't work, check the system paths in jupyter notebook:
import sys
sys.path
and the system executable:
sys.executable
These must correspond to the python in your current loaded environment.
For me, the issue was with the jupyter Notebook's kernel. See the kernel specifications in kernel.json file in the path. You can find the directory for this file from jupyter kernelspec list. I manually changed the python path to the python in my environment (this is a bad idea, but it worked).
Make sure that your jupyter notebook is finding the same version of python as your terminal, otherwise installing modules with conda install in your terminal won't show up in your notebook. Do
import sys
print(sys.version)
in your notebook and in your terminal. If they do not match up, then add your terminal's python version to your notebook:
conda install nb_conda_kernels
conda install ipykernel
and then in the notebook switch to the kernel you just installed (kernel -> change kernel)
Check your path for Python and Jupyter:
import sys
sys.path
You may find different results from Python and Jupyter. This can be fixed temporarily by appending this line of code if you are using macos:
sys.path.append('/Users/**your_user_name**/anaconda3/lib/python3.7/site-packages')
If you want to solve this permanently, you can create an iPython profile and fix it there.
First activate the finds environment, second launch jupyter notebook, third import sklearn. Inside jupyter notebook.
I have also found the same issue
ModuleNotFoundError: No module named 'sklearn'
but after searching I found its best solution.
You should also try the following steps:
Step 1:
open "cmd"
Step 2:
write "pip install notebook"
Step 3:
After installation of notebook, write "jupyter notebook " in cmd.
Tell me if you got your solution
thank you!
I'm about to give up on Anaconda. I never had trouble managing my packages with pip and I just thought I'd try it since now there's one package I can't get with pip and I'd heard so many good things about it.
I can't import a package I just installed with Anaconda, similar to this but on MacOS instead of Windows.
I really don't want multiple environments unless I have to have them. I want to be able to run most/all of my packages from the same scripts. I have a virtual environment named py37 where I've been putting most things. Among other packages:
(py37) jennifers-mbp:~ jenniferlongdiaz$ conda list
#packages in environment at /anaconda3/envs/py37:
#
# Name Version Build Channel
matplotlib-venn 0.11.5 py_1 conda-forge
numpy 1.15.3 py37h6a91979_0
python 3.7.1 haf84260_3
Python goes to the right installation:
(py37) jennifers-mbp:~ jenniferlongdiaz$ which python
/anaconda3/envs/py37/bin/python
(py37) jennifers-mbp:~ jenniferlongdiaz$ python
Python 3.7.1 (default, Oct 23 2018, 14:07:42)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
I can import numpy but not matplotlib-venn:
>>> import numpy as np
>>> import matplotlib_venn as venn
...bunch of stuff and then:
ImportError: cannot import name 'get_backend' from 'matplotlib' (/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/__init__.py)
Similiarly:
>>> from matplotlib import get_backend
...bunch of stuff and then:
ImportError: cannot import name 'get_backend' from 'matplotlib' (/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/__init__.py)
From within the IDE spyder ((py37) jennifers-mbp:~ jenniferlongdiaz$ spyder), I get:
In [1]: import matplotlib_venn as venn
Traceback (most recent call last):
File "<ipython-input-9-aafbc15b97e7>", line 1, in <module>
import matplotlib_venn as venn
ModuleNotFoundError: No module named 'matplotlib_venn'
Please help!
According to matplotlib-venn's PyPi page (https://pypi.org/project/matplotlib-venn/), the import should look like this:
import matplotlib_venn as venn
Note that the module is named with an underscore whereas the package is named with a dash; this is a tricky inconsistency
Update for updated question: the issues with spyder were due to spyder not being installed as part of anaconda, resulting in the system's spyder not being aware of the anaconda environment's packages. Being unable to import get_backend from matplotlib would suggest either a missing or borked matplotlib installation. Both just require installing (or reinstalling) the packages using conda
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.
My Ubuntu 16.04 LTS laptop refuses to import pandas. I have tried re installing and updating pandas countless times. I use a list to load all my libraries. To make sure there was not a conflict in libraries or compatibility issue I reloaded every element on my script on this local machine AND simultaineously on an Ubuntu 16.04.4 x64 cloud server. When I tried to import pandas on the cloud server it worked no problem. When I tried to import pandas on my local machine I continued to get this error.
Then I tried installing pandas using pip instead of conda and it still refused to import however the error changed. Please let me know if you have any suggestions or need any additional information.
import pandas as pd
ModuleNotFoundError Traceback (most recent call last)
ModuleNotFoundError: No module named 'pandas.core.dtypes.common'
=====================================================================
After pip install:
import pandas as pd
ImportError Traceback (most recent call last)
ImportError: cannot import name 're_type'
You must be using different versions of python. Perhaps both Python 2 and Python 3
Check the version of Python and install it in the same.
To install within python 2 use pip command
To install within python 3 use pip3 command
Check python version by
python --version
Python 3.6.5
OR
>>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32