Jupyter on mac complains "No module named pandas" - python

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!!

Related

ModuleNotFound Error on installed package in Jupyter Notebook environment

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!

Pandas not found despite installation - Mac M1

I have a new MacBook Pro M1 with mini-forge, arch i386 and am trying to run Pandas without success.
As an example, I have a python script with "import pandas as pd" (line 1) which returns error because it can not find Pandas.
File "test.py", line 1, in
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
I have tried to run (and installed) Pandas both in and outside of en environment installed Pandas both in and outside of an environment but none is recognised and returns the error.
I have installed Pandas - Thats not the problem here.
Do you have any idea of possible solutions for such problem?
The simplest approach for installing pandas is to install it as part of the Anaconda distribution
conda create - n name_of_my_env
source activate name_of_my_env
activate name_of_my_env
conda install pandas
conda install pandas = 0.20 .3
As seen here
You need to install numpy first in your environment, this worked for me:
python3 -m pip install virtualenv
virtualenv -p python3.8 venv
source venv/bin/activate
pip install --upgrade pip
pip install numpy cython
git clone https://github.com/pandas-dev/pandas.git
cd pandas
python3 setup.py install
As seen here: how to install pandas on m1 Mac
How did you install pandas? As I also have M1 Macbook, it is no matter.
Maybe you can check if directory which you installed pandas is in sys.path directories.

ImportErrorWhenRunningHook: pyinstaller failed to import module _pyinstaller_hooks_0_pandas_io_formats_style required for module

I want to create an .exe file and get this error: Failed to import module _Pyinstaller_hooks_0_pandas_io_formats_style required for module C:xxxxxx\site-packages\Pyinstaller\hooks\hook-pandas.io.format.style.py.
I am not familiar with this subject at all and couldt find anything that could resolve the issue by using google. Any help would be very much appreciated!!
This happened when freezing pandas inside conda environment using pyinstaller.
I had pandas installed using conda environment and also using pip like pip install pandas.
I removed the pip version and installed using conda, and that worked!
pip uninstall pandas
conda install -c conda-forge pandas

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

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!!

Categories