Running Jupyter laptop in virtualenv: installed sklearn module unavailable

I installed the created virtualenv machinelearn and installed several python modules (pandas, scipy and sklearn) in this environment.

When I run jupyter notebook, I can import pandas and scipy into my laptops. 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 can import all the modules on the command line, so I know that they were successfully installed:

 (machinelearn) me@yourbox :~/path/to/machinelearn$ python -c "import pandas, scipy, sklearn" (machinelearn) me@yourbox :~/path/to/machinelearn$ 

How to import sklearn in my jupyter laptop running in virtualenv?

+6
source share
4 answers

You probably haven't installed jupyter / IPython in your virtualenv. Try the following:

 python -c "import IPython" 

and make sure the jupyter command found in your $PATH is the one in the bin folder of your venv:

 which jupyter 

For Windows users in the powershell console, you can use the following to verify that the jupyter command in your $env:Path is the one located in the scripts folder of your venv:

 get-command jupyter 

Edit : if this is a problem, just run python -m pip install jupyter in your vein.

+4
source

Another approach is to have one global jupyter installation, but point to different kernels to work as a backend.

This approach is outlined here in its docs: http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs

Copying below in case of link breaking: You can use virtualenv for your IPython laptop. Follow these steps:

Install ipython kernel module in 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 accordingly.

Now you can see your kernel in the IPython notebook menu: Kernel β†’ Change kernel and be able to switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel will use for this laptop from now on.

+5
source

Creating virtualenv using the python3 -m venv command

I had the same problem as you. In my case, I created virtualenv using the command

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

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

To install jupyter (and in the first instance, which is not installed in your virtual environment using this command), but still has access to the system-site-package, which you can run:

 python3 -m venv ./my_virtual_env 

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

After deactivating and reactivating your environment, you will gain access to the system package sites.

Creating a virtual virtual machine using the virtualenv command

Given this answer , you can prevent access to system package sites by creating a file. / my _virtual_env / lib / python3.4 / no-global-site-packages.txt and get access back by deleting it.

0
source

To use the Jupyter laptop with a virtual environment (using virtualenvwrapper) and packages installed in this environment, follow these steps:

  • create a virtual environment

     mkvirtualenv --no-site-packages --python=/your/python/path your_env_name 
  • Activate virtual environment

     workon your_env_name 
  • Install Jupyter and other packages

     pip install jupyter, numpy 
  • Add new kernel to Jupyter configuration

     ipython kernel install --user --name=your_env_name 
  • Done. Now you can use your Jupyter laptop under a virtual environment.

     jupyter-notebook 

Disclaimer: the question was answered, but it is hidden in one of the answers. I searched Google and took the time to find the right answer. So I just summarize it so that someone with the same problem can easily follow.

0
source

Source: https://habr.com/ru/post/1015258/


All Articles