Homunculus Reticulli
Homunculus Reticulli

Reputation: 68366

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?

Upvotes: 89

Views: 135675

Answers (10)

damdafayton
damdafayton

Reputation: 2419

Those with VScode, can click Python version box on the right > "Select Another Kernel" > "Python environments" and select the one that has their venv path.

enter image description here

Upvotes: 0

NWMT
NWMT

Reputation: 185

Here is a more self-contained solution. Step 5 is the what is different from all other answers.

  1. Create the virtual environment

    python -m venv envname

  2. Activate virtual environment

    source path_to_envname/bin/activate

  3. Install packages (in the new virtual environment)

    pip install scikit-learn jupyter

  4. Locate the jupyter exectuable in path_to_envname/bin/

  5. Run the version of jupyter that is inside your virtual environment

    path_to_envname/bin/jupyter notebook

This way you don't muck up the "global" jupyter notebook

Upvotes: 2

hack3rman
hack3rman

Reputation: 1

Assuming you've set up the environment properly as others have noted, make sure you have installed the proper scikit-learn package. You can check with:

pip list

If you see something like: sklearn 0.0.post1

You need to run:

pip install scikit-learn

The following is depreciated and will not work:

pip install sklearn 

Upvotes: 0

ogrisel
ogrisel

Reputation: 40149

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

Upvotes: 103

user_3pij
user_3pij

Reputation: 1402

Solution without adding a new kernel globally!!

  1. create a new virtual environment by
python3 -m virtualenv envname

  1. 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.

  1. Install your desired packages in activated virtualenv and launch jupyter in it and voila!

Upvotes: 6

Pujan Mehta
Pujan Mehta

Reputation: 498

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!

Upvotes: 19

Vijay Kumar
Vijay Kumar

Reputation: 1

You can still install jupyter inside your virtual-environment if you have created your virtual env using:

python -m venv --system-site-packages path/to/my-venv

Simply do this:

activate-your-env
pip install -I jupyter

And you are now ready to go

jupyter notebook

Upvotes: -1

C. Feng
C. Feng

Reputation: 491

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.

Upvotes: 40

Tiphaine Champetier
Tiphaine Champetier

Reputation: 353

Creation of virtualenv with python3 -m venv command

I had the same problem as yours. In my case I had created the virtualenv with the command

python3 -m venv ./my_virtual_env --system-site-packages

The problem was I could not install jupyter inside the virtual environment as it was already in the system-site-package (when you try to install it, it tells you "Requirement already satisfied").

To install jupyter, (and in a first instance pip, that does not get installed neither in your virtual environment with this command) but still have access to system-site-package you can run :

python3 -m venv ./my_virtual_env

Activate you virtual environment, run pip3 install jupyter (and pip3 install pip) and then turn on the option include-system-site-packages in the file ./my_virtual_env/pyvenv.cfg.

After deactivation and reactivation of you environment, you will have access to system site-packages.

Creation of virtualenv with virtualenv command

Given this answer you can prevent the access to system site-packages by creating a file ./my_virtual_env/lib/python3.4/no-global-site-packages.txt, and get the access back by removing it.

Upvotes: 2

ClimbsRocks
ClimbsRocks

Reputation: 1144

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.

Upvotes: 60

Related Questions