I want to import numpy and pandas in jupyter but I get the message:
----> 1 import pandas as pd
ModuleNotFoundError: No module named 'pandas'
Now when I open the anaconda prompt and do pip list then I can see the latest version of numpy
Even when I say pip install it says requirement already satisfied.
I know that this relates in some way to the PATH of python.
Honestly as someone from a math background learning how to code I'm really unsure what this means and how I can check and fix whether the Path is correct or not.
Does it just mean where jupyter looks for python.exe? I dont know for sure where to check for this.
where python in the Anaconda Prompt gives me:
C:\Users\MyName\Anaconda3\python.exe
C:\Program Files\Python37\python.exe
When I run this on the jupyter notebook:
from jupyter_core.paths import jupyter_data_dir
print(jupyter_data_dir())
I get:
C:\Users\NyName\AppData\Roaming\jupyter
Is this the source of the problem?
The problem is that your Jupyter kernel is using a different Python from the one in your Anaconda prompt.
If you don't use virtual environments, you should start. To make one, open an Anaconda prompt and do this (changing the name myenv to whatever you like):
conda create -n myenv python=3.7 jupyter matplotlib pandas
Change the version of Python or the other packages too if you want.
When that's finished, switch to that environment:
conda activate myenv
Now do this:
python -m ipykernel install --user --name myenv
This adds a Jupyter kernel for this environment. You only need to do this once, after creating the environment.
Now restart Jupyter notebook or Jupyter lab or whatever. Or install more stuff in this environment if you want. You can just use pip like so:
pip install awesomepackage
You should now see your environment under Kernel > Change kernel, and under New when making a new notebook.
Forgive me if you know all this, but this is the only way I've been able to keep environments straight, and to know exactly where I'm installing stuff. Good luck!
So the solution I've found is that simply using pip install is incorrect.
This link here shows the way it has to be done. By using the command:
!{sys.executable} -m pip install numpy
I am correctly able to import the package and use it.
If anyone could help me understand how this command works that'd be super helpful. This has solved my problem but I don't really know why or how.
Related
I tried installing Rasterio library using command- pip install rasterio and also used conda commands in anaconda cmd, both are didn't worked and it's taking too long to install so I aborted the process. Let me know in detail how to install raserio library for python?
I tried with installing GDAL and raterio whl file by using pip commands. It got installed, but when I tried to import module- it's giving me an error saying No module found. Please help to fix this.
Thanks in advance
I just had the similar problem, and tl:dr the issue for me was multiple environments, like Ameya said. Here's a longer version of Ameya's spot-on answer with steps to diagnose and fix.
Without realizing it, I had two environments going on: my jupyter notebook was running python 3.10 and my global python was running 3.9, and each was looking for site-packages in different locations (/opt/homebrew/Cellar/jupyterlab/3.4.7/libexec/lib/python3.10/site-packages vs /Users//Library/Python/3.9/lib/site-packages).
This happened because I had trouble with getting python and specifically jupyterlab running on Monterey and the only fix for me was using homebrew to manage the packages. Anything I installed with brew from the command line, went into /opt/homebrew/Cellar... etc and could be seen by my jupyter notebook. Anything I used pip install to get from within an open notebook also went onto this path that my notebook could see. But anything I used pip install from the command line to get, went to the path of the global environment's site packages. Then, my jupyter notebook couldn't see them.
You don't mention that you are using jupyter notebook but perhaps something analogous could happen between multiple environments for you.
You can check if this is the case for you by doing the following:
start python from the command line
import sys
run sys.path
start jupyter notebook, or start python from your other environment
same thing, import sys, run sys.path
Are they the same? If not, probably pip is putting your rasterio in other python env site-packages.
To fix, you can either pip install from within your preferred environment, or copy and paste the site packages corresponding to rasterio from one site-packages location to the other.
I have a Jupyter notebook running inside an Anaconda virtual environment that won't recognize the statsmodels package. I've used both pip and conda, and I've reinstalled the whole environment several times in between restarting my computer. As you can see in this link, both pip and conda show that it is installed. However, I continue to get this error:
---> 43 import statsmodels.api as sm
ModuleNotFoundError: No module named 'statsmodels'
At first I thought it was the kernel, but that wouldn't explain why every other package I've installed is recognized properly.
The only thing I haven't done is tried it on another computer (I'm running Windows 10 x64). I just want to know if anyone can see something that I'm missing before I do so.
Thanks!
Edit:
I've tried the following commands to install it:
pip install statsmodels
conda install -c conda-forge statsmodels
conda install statsmodels
I've tried using each command individually, making sure to uninstall the previous installation. I've tried using only two at a time, with the same methodology. I've tried every permutation of the above installations, with a computer restart in between. I don't think the problem is that it's not installing. I have no clue why but Jupyter notebooks just isn't recognizing this one package.
I'm running all of my commands from an Anaconda command prompt, and I make sure to activate my focal virtual environment. I also run Jupyter lab by typing it directly into the command prompt and hitting enter (I make sure I've activated my environment).
I'll try running it on a different OS and update.
Thanks duffymo! My notebook wasn't using the correct kernel. I thought all I needed to do was run Jupyter lab from my virtual environment, but it needs to be purposefully added. I followed the directions shown here to add my conda environment to Jupyter lab:
How to add conda environment to jupyter lab
I am having major trouble right now trying to use numpy in my jupyter notebook.
When I first tried to simply "import numpy", it came back with the error: "ModuleNotFoundError: No module named 'numpy'"
I then read somewhere that I probably needed to install numpy.
So I did this: "import sys
!conda install --yes --prefix {sys.prefix} numpy"
to which it came back saying: "EnvironmentLocationNotFound: Not a conda environment"
Now when it comes to understanding environments or packages or ANYTHING along those lines, I just have no idea what I'm doing. I am in a very beginner course and just following along.
I wish I could understand all of this environment and versioning stuff.
I have no idea where to go from here.
Any insight here would be GREATLY appreciated!!
Edit: I am in fact using Anaconda to launch Jupyter Notebook. Not sure if that means anything to your understanding of my problem or a potential solution.
Mark
For an easy to use graphical user interface to install Python libraries and manage environments, I would recommend Anaconda. It is well integrated with IPython and Jupyter Notebook in particular.
You could try the line bellow, at the notebook:
!pip install numpy
Or you could open up a terminal inside the jupyter notebook and install there, with:
pip install numpy
Another option is to install python3.7 from the anaconda website:
https://www.anaconda.com/distribution/
With anaconda a lot of packages come together when you install it. It's better for beginners. Good luck!
In my case, inside Jupyter notebook, you need to change Kernel (Anaconda environment). I thought you changed environment using conda activate myEnv, but when launching Jupyter, it defaults to the root environment.
I hope this is in fact true- I am a noob in Anaconda.
I could use some clarification regarding anaconda envs and the installed packages. I have just began using environments other than the base to keep my installations clean.
I just deleted my anaconda3 folder, installed it from scratch, made a new environment and tried to run a jupyter-notebook.
When reading excel via pandas I get the error Missing optional dependency 'xlrd'
prompting me to install xlrd via conda or pip.
So I open the terminal, activate the env I’m working in and install it using conda. I close the JN from the terminal, reopen it and run the same code. The problem persists.
I repeat the procedure, this time indicating the name of the env at the end. Problem persists.
I checked which python in the terminal, it is indeed the anaconda3/envs/newenv/bin/python
I do the same (in the new env) using pip. I use the command .../anaconda3/envs/newenv/bin/python -m pip install xlrd. I get the message Requirement already satisfied: xlrd in ./anaconda3/envs/newenv/lib/python3.7/site-packages (1.2.0).
I even tried installing the package inside the notebook using !conda install xlrd, still I get the same error.
Finally, I open Jupyter via the base env, and the package works there perfectly.
I have no idea why I can’t install this under the newenv, which was the point of having local envs after all.
Thanks heaps for your help!
You need to install the kernel in Jupyter to be able to use it.
jupyter kernelspec list
That command will give you the list of kernels you have. I am assuming it only shows you Python3
You will now need to install a kernel. Remember to do this while inside your virtual environment
python3 -m pip install ipykernel
python3 -m ipykernel install --user --name <your-new-kernel-name>
You should now see this in Jupyter notebook. Select the kernel in Jupyter and you should be good to go.
Another thing you may want to try is to install Jupyter while inside your virtual environment. While inside your virtual environment, you could do:
python3 -m pip install jupyter lab
and then while still inside your virtual environment, run jupyter after checking which jupyter. It should solve your problem as well.
I hope this question is not redundant, but I couldn’t find a solution on the internet so far. I’ve followed the github guide (https://github.com/fastai/fastai) to install the fastai library on my Anaconda environment. I want to use this library to tackle the ML course and solve other Kaggle competitions with it.
As per below, I’m opening my conda environment, selecting fastai and opening Jupyter Notebook:
When trying to run the packages in Jupyter, I still get an Import Error with No module named ‘fastai’. Any suggestions on what I’m doing wrong?
Had same issue. It looks like the problem is with "softlinks" fastai which are not working on Windows.
I fixed it with replacing that soflinks with real module fastai from root of repository. Just copied it to courses\dl1, courses\dl2, courses\dm1 and tutorials folders.
Prerry dirty. But works. Let me know how to fix it without copying.
Add a cell to the beginning of the notebook, !python -m pip install git+https://github.com/fastai/fastai.git
Edit:
Now that fastai is 1.0 and on pip, you probably just want to !python -m pip install fastai.
I prefer the !python -m pip syntax over just calling pip when mixing anaconda and pip, but it probably just reflects my system's python being messed up.
I had trouble making fastai_v1 work in jupyter lab, however it was working on the terminal. So the problem was with the virtual environment not being selected in the jupyter lab when it was launched.
I used the below code to make it work. Replace 'myenv' with your virtual environment name.
source activate myevn
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
jupyter lab
more info here: Conda environments not showing up in Jupyter Notebook