Problem importing (and installing) NumPy in Jupyter Notebook - python

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.

Related

Installing Rasterio

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.

Pandas not working even though its installed

I've installed python and pandas using the Anaconda library, but it doesn't work when I try to import pandas in Jupyter Notebook or in the Python Idle. It does work when I run the shell in the terminal.
I am using macOS Mojave and Python version 3.7.6.
In the terminal, it says I have pandas already installed as you can see below.
However, I get the error message "ModuleNotFoundError: No module named 'pandas'" when trying to import it in the Python Idle as you can see below.
I think I know where the problem comes from but I don't know how to troubleshoot or fix it. I installed Anaconda 2 years ago and used it but deleted it when I was done with it to make space in my computer and now I reinstalled it. So I think the Idle and Jupyter Notebook are using a different version of Python than the one that came in the Anaconda package. I might be completely wrong.
Thank you for your help!
You probably haven't installed pandas in the same environment your Jupyter kernel is running in.
You can install it directly from Jupyter notebook by running !pip install pandas. That will install it in the environment that the kernel started in.
In general, running !pip freeze from jupyter notebook should show you all installed libraries. If pandas is not there after you ran !pip install pandas, your environment paths are broken in some big way.
In that case, I'd suggest nuking anaconda and jupyter installation and starting again.
If you want to know more about kernels and how packages work in them https://biasandvariance.com/importing-packages-in-jupyter-notebook/ could help.
If you want to use Anaconda, then just do:
conda install pandas
Mixing conda and pip may cause issues.
Can you try updating pandas?
pip install --upgrade pandas

Requirements are satisified, but unable to import libraries in jupyter notebook

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.

Cannot run fastai library on Jupyter Notebook

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

ModuleNotFoundError: No module named 'sklearn'

I want to import sklearn but there is no module apparently:
ModuleNotFoundError: No module named 'sklearn'
I am using Anaconda and Python 3.6.1; I have checked everywhere but still can't find answers.
When I use the command:
conda install scikit-learn should this not just work?
Where does anaconda install the package?
I was checking the frameworks in my python library and there was nothing about sklearn only numpy and scipy.
Please help, I am new to using python packages especially via anaconda.
You can just use pip for installing packages, even when you are using anaconda:
pip install -U scikit-learn scipy matplotlib
This should work for installing the package.
And for Python 3.x just use pip3:
pip3 install -U scikit-learn scipy matplotlib
Will leave below two options that may help one solve the problem:
Using conda
Using pip
One might want to consider the notes at the end, specially before resorting to the 2nd option.
Option 1
If one wants to install it in the root and one follows the requirements - (Python (>= 2.7 or >= 3.4), NumPy (>= 1.8.2), SciPy (>= 0.13.3).) - the following should solve the problem
conda install scikit-learn
Alternatively, as mentioned here, one can specify the channel as follows
conda install -c anaconda scikit-learn
Let's say that one is working in the environment with the name ML.
Then the following should solve one's problem:
conda install -n ML scikit-learn
# or
conda install -n ML -c anaconda scikit-learn
Option 2
If the above doesn't work, on Anaconda Prompt one can also use pip (here's how to pip install scikit-learn), so the following may help
pip install scikit-learn
However, consider the last note below before proceeding.
Notes:
When using Anaconda, one needs to be aware of the environment that one is working.
Then, in Anaconda Prompt, one needs to run the following
conda $command -n $ENVIRONMENT_NAME $IDE/package/module
$command - Command that one intends to use (consult documentation for general commands)
$ENVIRONMENT NAME - The name of one's environment (if one is working in the root,
conda $command $IDE/package/module is enough)
$IDE/package/module - The name of the IDE or package or module
If one needs to install/update packages, the logic is the same as mentioned in the introduction. If you need more information on Anaconda Packages, check the documentation.
What is the flag -c.
pip doesn't manage dependencies the same way conda does and can, potentially, damage one's installation.
If you are using Ubuntu 18.04 or higher with python3.xxx then try this command
$ sudo apt install python3-sklearn
then try your command. hope it will work
I did the following:
import sys
!{sys.executable} -m pip install sklearn
I've tried a lot of things but finally, including uninstall with the automated tools. So, I've uninstalled manually scikit-learn.
sudo rm -R /home/ubuntu/.local/lib/python3.6/site-packages/sklearn
sudo rm -R /home/ubuntu/.local/lib/python3.6/site-packages/scikit_learn-0.20.0-py3.6.egg-info
And re-install using pip
sudo pip3.6 install -U scikit-learn
Hope that can help someone else!
This happened to me, I tried all the possible solutions with no luck!
Finaly I realized that the problem was with Jupyter notebook environment, not with sklearn!
I solved the problem by re-installing Jupyter at the same environment as sklearn
the command is: conda install -c anaconda ipython. Done...
The other name of sklearn in anaconda is scikit-learn. simply open your anaconda navigator, go to the environments, select your environment, for example tensorflow or whatever you want to work with, search for scikit_learn in the list of uninstalled packages, apply it and then you can import sklearn in your jupyter.
SOLVED:
The above did not help. Then I simply installed sklearn from within Jypyter-lab, even though sklearn 0.0 shows in 'pip list':
!pip install sklearn
import sklearn
What I learned later is that pip installs, in my case, packages in a different folder than Jupyter. This can be seen by executing:
import sys
print(sys.path)
Once from within Jupyter_lab notebook, and once from the command line using 'py notebook.py'.
In my case Jupyter list of paths where subfolders of 'anaconda' whereas Python list where subfolders of c:\users[username]...
On Windows, I had python 3+ version. pip version - 22.3.1
I had installed:
pip install sklearn
But, it seems it is deprecated with scikit-learn.
So, I did:
pip install scikit-learn
And, it worked!!!
Cause
Conda and pip install scikit-learn under ~/anaconda3/envs/$ENV/lib/python3.7/site-packages, however Jupyter notebook looks for the package under ~/anaconda3/lib/python3.7/site-packages.
Therefore, even when the environment is specified to conda, it does not work.
conda install -n $ENV scikit-learn # Does not work
Solution
pip 3 install the package under ~/anaconda3/lib/python3.7/site-packages.
Verify
After pip3, in a Jupyter notebook.
import sklearn
sklearn.__file__
~/anaconda3/lib/python3.7/site-packages/sklearn/init.py'
I had the same problem.
The issue is when we work on multiple anaconda environments, not all packages are installed in all environments.
you can check your conda environment by writing the following code in anaconda prompt:
conda env list
then you can check the packages installed in each environment :
conda list -n NAME_OF_THE_ENVIRONMENT
for me, the environment that I was working with , was missing sklearn, although the package was installed in the other environments.
therefore, I just simply installed sklearn package in that particular environment
conda install -n NAME_OF_THE_ENVIRONMENT scikit-learn
and the issue was resolved
install these ==>> pip install -U scikit-learn scipy matplotlib
if still getting the same error then ,
make sure that your imoprted statment should be correct. i made the mistike while writing ensemble so ,(check spelling)
its
should be >>> from sklearn.ensemble import RandomForestClassifier
I had the same issue as the author, and ran into the issue with and without Anaconda and regardless of Python version. Everyone's environment is different, but after resolving it for myself I think that in some cases it may be due to having multiple version of Python installed. Each installed Python version has its own \Lib\site-packages\ folder which can contain a unique set of modules for that Python version, and where the IDE looks into folder path that doesn't have scikit-learn in it.
One way to try solve the issue: you might clear your system of all other Python versions and their cached/temp files/system variables, and then only have one version of Python installed anywhere. Then install the dependencies Numpy and Scipy, and finally Scikit-learn.
More detailed steps:
Uninstall all Python versions and their launchers (e.g. from Control Panel in Windows) except the one version you want to keep. Delete any old Python version folders in the Python directory --uninstalling doesn't remove all files.
Remove other Python versions from your OS' Environment Variables (both under the system and user variables sections)
Clear temporary files. For example, for Windows, delete all AppData Temp cache files (in C:\Users\YourUserName\AppData\Local\Temp). In addition, you could also do a Windows disk cleanup for other temporary files, and then reboot.
If your IDE supports it, create a new virtual environment in Settings, then set your only installed Python version as the interpreter.
In your IDE, install the dependencies Scipy and Numpy from the module list first, then install Scikit-Learn.
As some others have suggested, the key is making sure your environment is set up correctly where everything points to the correct library folder on your computer where the Sklearn package is located. There are a few ways this can be resolved. My approach was more drastic, but it turns out that I had a very messy Python setup on my system so I had to start fresh.
Using Anaconda-navigator UI environment
When running Anaconda-navigator:
Choose the 'Environments' tab on the left and create a new environment (e.g. ML - see Gonçalo Peres answer above, I made one called 'CourseraML').
Set Python version 3.7 (for Coursera course Applied Machine Learning in Python). Also include R.
Then find modules to install using the 'not installed' drop-down menu item. Search for each module needed in the search bar and select. sklearn is part of scikit-learn. Select it and install (it should find all relevant dependencies). Modules needed for Applied ML course: seaborn, numpy, scikit-learn, pandas, matplotlib
You'll need to restart Jupyter Notebook and reopen your file.
Command line version of above:
conda install -n CourseraML seaborn scikit-learn pandas numpy matplotlib graphviz
Causes
-your jupyter notebook might be importing the sklearn and other libraries from the
another the location(path) than the libraries from conda or pip.
MY Problem
In my case, My jupyter notebook was importing the libraries for snap manager. Since, I install jupyter using the snap instead of other ways.
You can check where other libraries are being imported in jupyter using code:
import cv2 as cv
print(cv.__file__)
Solution
So , I uninstall jupyter notebook and then install notebook using conda.
sudo snap remove jupyter
conda install -c conda-forge notebook

Categories