Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Jupyter notebook in a virtualenv: installed sklearn module not available

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?

like image 917
Homunculus Reticulli Avatar asked Feb 24 '17 23:02

Homunculus Reticulli


People also ask

Can not perform a '-- user install user site packages are not visible in this Virtualenv?

If this command returns Can not perform a '--user' install. User site-packages are not visible in this virtualenv. then you are already in a Python virtual environment. You will need to deactivate this environment or contact your system administrator to install virtualenv for you in this environment.

Do I need to install Jupyter Notebook for each environment?

Jupyter Notebook can easily be installed using conda. Our plan is to only install it in the base environment, and then just switch between sub-environments to avoid setting up Jupyter Lab in each environment.


3 Answers

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
like image 54
ogrisel Avatar answered Oct 14 '22 06:10

ogrisel


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.

like image 42
ClimbsRocks Avatar answered Oct 14 '22 05:10

ClimbsRocks


To use Jupyter notebook with virtual environment (using virtualenvwrapper) plus packages installed in that environment, follow steps below:

  1. create a virtual environment

    mkvirtualenv --no-site-packages --python=/your/python/path your_env_name
    
  2. Activate the virtual environment

    workon your_env_name
    
  3. Install Jupyter and other packages

    pip install jupyter, numpy
    
  4. Add a new kernel to your Jupyter config

    ipython kernel install --user --name=your_env_name
    
  5. 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.

like image 28
C. Feng Avatar answered Oct 14 '22 06:10

C. Feng