ModuleNotFound Error on installed package in Jupyter Notebook environment - python

Using Python Jupyter notebooks to work in an environment I've created. Everything went fine until I decided to add another package to the environment. After setting up the environment, I was unable to import additional packages I installed in the environment. I was only able to import packages I installed on set-up.
Here are the commands I used to create the environment initially:
python -m venv test
. test/bin/activate
pip install pyodbc
deactivate
python3 -m ipykernel install --user --name test
This all worked fine. I started a notebook using the new environment and was able to import pyodbc without error!
Then I decided I wanted to add another package, geopandas.
. test/bin/activate
pip install geopandas
deactivate
I opened a notebook in the kernel and tried
import geopandas
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_47903/1529612126.py in <module>
----> 1 import geopandas
ModuleNotFoundError: No module named 'geopandas'
I then attempted linking it to ipykernel again
python3 -m ipykernel install --user --name test
But I still received the same error in the notebook and was unable to import the package. How can I add more packages to an environment after already establishing it? I'm not sure what I'm doing wrong. Thank you!

Related

Jupyter on mac complains "No module named pandas"

Here are the steps that I did:
I installed anaconda on my mac.
I created a new environment using conda create -n myenv
I activated this environment using conda activate myenv
I installed several modules including pandas
I run conda list and it showed pandas 1.1.5 py38hb2f4e1b_0
I launched jupyter from anaconda dashboard
In the new notebook, I can see my new environment myenv, so I created a new notebook using myenv
In the notebook, I run this code import pandas as pd and it throws me this error:
ImportError Traceback (most recent call last)
in ()
1 import numpy as np
----> 2 import pandas as pd
ImportError: No module named pandas
I checked anaconda dashboard environment then check the installed modules under myenv and pandas is there. So i'm wondering why it cannot find the module.
I tried running !pip install pandas --upgrade in my opened myenv notebook and it showed this results
Installing collected packages: pandas
Attempting uninstall: pandas
Found existing installation: pandas 1.1.3
Uninstalling pandas-1.1.3:
Successfully uninstalled pandas-1.1.3
Successfully installed pandas-1.1.5
Now I'm lost why it upgraded the pandas from 1.1.3 to 1.1.5 while the version of pandas in myenv environment is already 1.1.5
How can I get pandas to run in my environment correctly?
Try to use it in Notebook. It will help you to know if you're using the correct env(python) or your default system installed python.
**!which python**
**!pip freeze**
Most probably you'll find out that you're using system python
then try to run this command on the terminal after activating your env.
**python -m ipykernel install --user --name=YourEnvName**(or any Name)
and you'll definitely see a new kernel in your notebook by the name "YourEnvName"
Check the image below
Thanks!!

Jupyter not recognising numpy (using homebrew pyenv)

I followed these instructions to get a python virtual environment (managed by pyenv) working in a Jupyter Notebook.
In summary, I did
brew install pyenv
pyenv install 3.8.5
pyenv virtualenv 3.8.5 myproject
pyenv shell myproject
pip install jupyter notebook matplotlib pandas numpy scipy
Then I started up jupyter notebook from terminal (I tried both jupyter notebook and jupyter lab). When I typed import numpy as np, I get
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-0aa0b027fcb6> in <module>
----> 1 import numpy as np
ModuleNotFoundError: No module named 'numpy'
But in the very next cell, if I evaluate !pip install numpy, I get
Requirement already satisfied: numpy in /Users/<my_username>/.pyenv/versions/3.8.5/envs/myproject/lib/python3.8/site-packages (1.19.1)
And also, !which python and !which pip return
/Users/<my_username>/.pyenv/shims/pip
/Users/<my_username>/.pyenv/shims/python
How is it even possible that in the very same jupyter notebook, it can't find numpy but pip says numpy is already installed? I can never seem to get my Python environments to just behave!
I had the exact same issue. I think it's all due to your "varying pip list". You probably have installed bunch of libraries via pip install before you created the shims path (prior your pyenv installation). You may also have installed some others after the shims creation. So your jupyter-notebook and jupyter-lab may not have access to the priorly installed libraries.
Since you have installed pyenv via homebrew, without touching anything else just re-installing the jupyterlab via homebrew might take care of it.
I used this:
brew install jupyterlab
And it solved my problem.
source: https://formulae.brew.sh/formula/jupyterlab

Pycharm throws: ModuleNotFoundError: No module named 'numpy' error even though Numpy is installed

When try to import numpy in pycharm it throws: ModuleNotFoundError: No module named 'numpy' error even though numpy is installed on the machine.
If I try to run the console it works perfectly.
pycharm creates its own virtual environment. But you have installed numpy in your system environment.
just open pychram console and run pip install numpy
There are two ways:
Easy way: Use GUI
File --> Settings --> Project Interpreter
Next you search for Numpy package and install desired package
Hard Way: Use command prompt
Go to terminal and upgrade pip first by typing the following
python -m pip install --upgrade pip
Next, install the package
pip install numpy
Verification
After installation is complete, please verify by typing following commands in console
import numpy as np
print (np.__version__)
Open pycharm console
Open File, Select ---> Settings, Select ---> Project Interpreter
Open the terminal and run the below command to upgrade pip.
python -m pip install --upgrade pip
run the commands to install the package
pip install numpy

No module found error for every conda package in anaconda jupyter notebook

After installing the latest scikit-learn version (19) using conda in Anaconda Jupyter, all packages are showing ModuleNotFoundError - scikit learn, numpy, matplotlib, etc. This is happening for all conda packages. After uninstalling and reinstallling, I have the same problem.
My kernel specs :
sana#skb-linux:~$ . activate my_env
(my_env) sana#skb-linux:~$ jupyter kernelspec list
Available kernels:
python3 /home/sana/anaconda3/envs/my_env/share/jupyter/kernels/python3
My code:
# scipy
import scipy
print('scipy: %s' % scipy.__version__)
Result:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-73263c49dde4> in <module>()
1 # scipy
----> 2 import scipy
3 print('scipy: %s' % scipy.__version__)
ModuleNotFoundError: No module named 'scipy'
You can check for the install packages by typing !conda list in the notebook and check for scikit-learn package is there or not.
If it's not present then you can install it by !pip install scikit-learn or !conda install -c anaconda scikit-learn
later try import sklearn it should work!.
There is a useful source here, including how to update or upgrade packages..
final solution: first need activate the environment you are working and install conda package in that particular environment using conda install and do need not to install outside of your environment
sana#skb-linux:~$ . activate my_env
(my_env) sana#skb-linux:~$ conda install scikit-learn
Solving environment: done
so the sklearn package is installed in your particular environment(in my case its installed my_env)
I encountered the same issue on installing jupyter through conda, tried a lot of things, didn't work out. Finally, I remove it and re-installed it, worked fine this time, lol!!

Cant import matplotlib on python 2.7.5

I installed matplotlib using (pip install --user matplotlib) and the installation was successful. when I try to import it using (import matplotlib) in python shell I get this error:
Traceback (most recent call last):
File "", line 1, in
import matplotlib
ImportError: No module named matplotlib
I couldn't find another question similar to mine because I'm not using anaconda.
Try pip uninstall matplotlib then python -mpip install matplotlib see if it helps. Otherwise, i'd recommend using a virtual environment which you can install using sudo easy_install virtualenv or pip install virtualenv This way you could just install and use matplotlib by following these steps:
Create a new virtual environment : user#yourmac$ virtualenv .env (.env can be any name or directory)
Activate it by doing : user#yourmac$ source .env/bin/activate
Then once it's activated just do a pip install matplotlib and use it to your satisfaction.
Hopefully this should solve your problem.

Categories