Running Jupyter notebook in a virtualenv: installed sklearn module not available
Solution 1
You probably have not installed jupyter / IPython in your virtualenv. Try the following:
python -c "import IPython"
and check that the jupyter
command found in your $PATH
is the one from the bin
folder of your venv:
which jupyter
For windows users in a powershell console, you can use the following to check that the jupyter
command in your $env:Path
is the one from the Scripts
folder of you venv:
get-command jupyter
Edit: if this is the problem, just run python -m pip install jupyter
in your venv.
Edit 2: actually you might also need:
python -m ipykernel install --user --name=my-virtualenv-name
and then switch the kernel named "my-virtualenv-name" in the jupyter user interface.
Edit 3: maybe the --user
flag in the last command is a bad idea:
python -m ipykernel install --name=my-virtualenv-name
Solution 2
Another approach to take is to have one global jupyter installation, but to point to different kernels to run as the backend.
That approach is outlined here in their docs: http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
Copying below in case the link breaks: You can use a virtualenv for your IPython notebook. Follow the following steps:
Install the ipython kernel module into your virtualenv
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install ipykernel
Now run the kernel "self-install" script:
python -m ipykernel install --user --name=my-virtualenv-name
Replacing the --name parameter as appropriate.
You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
Solution 3
To use Jupyter notebook with virtual environment (using virtualenvwrapper) plus packages installed in that environment, follow steps below:
-
create a virtual environment
mkvirtualenv --no-site-packages --python=/your/python/path your_env_name
-
Activate the virtual environment
workon your_env_name
-
Install Jupyter and other packages
pip install jupyter, numpy
-
Add a new kernel to your Jupyter config
ipython kernel install --user --name=your_env_name
-
Done. You may now use Jupyter notebook under the virtual environment.
jupyter-notebook
Disclaimer: the question has been answered but is hidden in one of the replies. I googled and took sometime to find the right answer. So I just summarize it so someone having the same issue can easily follow.
Solution 4
Assuming that jupyter is installed on your machine, not on the virtual environtment.
Using a virtual environment with Jupyter notebook
VENV_NAME = "YOUR VIRTUAL ENV NAME"
1) virtualenv VENV_NAME
2) source venv/bin/activate
3) Add this package if not present: pip3 install ipykernel
4) Then execute this command: ipython kernel install --user --name=VENV_NAME
5) Now open up the Jupyter Notebook and in change kernel select VENV_NAME
6) To install a new package perform pip3 install <PACKAGE NAME>
in your terminal and repeat step 4.
Hope it helps!
Solution 5
Solution without adding a new kernel globally!!
- create a new virtual environment by
python3 -m virtualenv envname
- Activate your enviroment and install jupyter in it by
pip install jupyter
One thing you have to make sure before installing jupyter is that you don't have following packages already installed in it.
ipykernel
ipython
ipython-genutils
ipywidgets
jupyter
jupyter-client
jupyter-console
jupyter-core
If you've previously installed them then first uninstall them by pip uninstall
.
- Install your desired packages in activated virtualenv and launch jupyter in it and voila!
Related videos on Youtube
Homunculus Reticulli
Updated on July 09, 2022Comments
-
Homunculus Reticulli almost 2 years
I have installed a created a virtualenv machinelearn and installed a few python modules (pandas, scipy and sklearn) in that environment.
When I run jupyter notebook, I can import pandas and scipy in my notebooks - however, when I try to import sklearn, I get the following error message:
import sklearn --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-1-8fd979e02004> in <module>() ----> 1 import sklearn ImportError: No module named 'sklearn'
I am able to import all modules, at the command line - so I know they have been successfully installed:
(machinelearn) me@yourbox:~/path/to/machinelearn$ python -c "import pandas, scipy, sklearn" (machinelearn) me@yourbox:~/path/to/machinelearn$
How can I import sklearn in my jupyter notebook running in a virtualenv?
-
joydeep bhattacharjee about 7 yearsI think you have not started your notebook with the correct python executable. Its written towards the right of the page and there should be a dropdown. Check if it is the conda installable with the virtualenv and not root.
-
-
Homunculus Reticulli about 7 yearsInteresting, I don't have IPython installed in the virtenv, but running
which jupyter
shows its in/usr/bin/jupyter
, Ill try pip installing Jupyter in the virtenv and see if that resolves the issue. -
Homunculus Reticulli about 7 yearsNope, installing jupyter in my virtenv did not reolve the issue, I still get the error message:
ImportError: No module named 'sklearn'
-
Homunculus Reticulli about 7 yearsActually, I created a new virtenv (--no-site-packages) and pip installed the modules I wanted - now it works. Thanks
-
ogrisel about 7 years> ImportError: No module named 'sklearn' Then it actually did resolve the jupyter issue. You just need to
pip install scikit-learn
in your venv as well. -
Elliptica over 5 yearsI can see the kernel find and connect to it in jupyter, but it can't find my modules still. Ideas?
-
andilabs over 5 yearsthe answer of @ClimbsRocks
Now run the kernel "self-install" script: python -m ipykernel install --user --name=my-virtualenv-name Replacing the --name parameter as appropriate.
solved the problem for me -
cglacet about 5 yearsDon't forget to switch kernel in jupyter.
-
Valentas about 5 years
ipykernel install
works, but unfortunately adds a new kernel globally, i.e. on sessions not run within this virtual env. -
cglacet almost 5 yearsYep, that's a problem for sure.
-
xtof54 over 4 yearsInstalling jupyter in virtualenv is not working here: it fails to connect to the kernel
-
Jed over 4 years@climbsrocks yea, as Elliiptica has stated, I've done this and still not able import packages. Any ideas?
-
Cornelius Roemer almost 4 yearsAmazing, this worked directly for me. In step 1 the --python flag was unnecessary for me (didn't know what to put there so left it out), also step 2 was redundant for me.
-
MrR almost 4 yearsWhere is 5 performed? In the activated VENV_NAME? why should I repeat step 4 every time I install a package??
-
Roger Steinberg almost 3 yearsthats the real answer... the order is crucial to making this work.
-
Kriticoder almost 3 yearslaunching jupyter-notebook from the same terminal where my env was activated did the trick for me..thanks!!!
-
chiceman over 2 yearsIf it's not working for you, try running
python -m ipykernel install --name=my-virtualenv-name
from within your activated virtual environment. Note: I removed the--user
flag. This will install the kernelspec to the system directory. I'm not an expert on the topic, but I it seems to source the correct path when you launch jupyter-lab. Worked for me! -
jtlz2 over 2 yearsStep 4 makes this a golden answer - thank you so so much!! Highly bookmarkable.