简体   繁体   English

在 virtualenv 中运行 Jupyter notebook:安装的 sklearn 模块不可用

[英]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.我已经安装了创建的virtualenv machinelearn并安装在该环境中的几个Python模块(大熊猫,SciPy的和sklearn)。

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:当我运行 jupyter notebook 时,我可以在我的 notebook 中导入 pandas 和 scipy - 但是,当我尝试导入 sklearn 时,我收到以下错误消息:

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?如何在运行在 virtualenv 中的 jupyter notebook 中导入 sklearn?

You probably have not installed jupyter / IPython in your virtualenv.您可能还没有在您的 virtualenv 中安装 jupyter / IPython。 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:并检查在$PATH找到的jupyter命令是否来自 venv 的bin文件夹:

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:对于 powershell 控制台中的 Windows 用户,您可以使用以下命令检查$env:Path中的jupyter命令是否是您 venv 的Scripts文件夹中的命令:

get-command jupyter

Edit : if this is the problem, just run python -m pip install jupyter in your venv.编辑:如果这是问题所在,只需在您的 venv 中运行python -m pip install jupyter jupyter。

Edit 2 : actually you might also need:编辑 2 :实际上您可能还需要:

python -m ipykernel install --user --name=my-virtualenv-name

and then switch the kernel named "my-virtualenv-name" in the jupyter user interface.然后在 jupyter 用户界面中切换名为“my-virtualenv-name”的内核。

Another approach to take is to have one global jupyter installation, but to point to different kernels to run as the backend.另一种方法是安装一个全局 jupyter,但指向不同的内核作为后端运行。

That approach is outlined here in their docs:http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs他们的文档中概述了这种方法:http ://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs

Copying below in case the link breaks: You can use a virtualenv for your IPython notebook.在链接断开的情况下复制以下内容:您可以为您的 IPython 笔记本使用 virtualenv。 Follow the following steps:请按照以下步骤操作:

Install the ipython kernel module into your virtualenv将 ipython 内核模块安装到您的 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.根据需要替换 --name 参数。

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 notebook 菜单中看到您的内核:Kernel -> Change kernel 并能够切换到它(您可能需要在它出现在列表中之前刷新页面)。 IPython will remember which kernel to use for that notebook from then on.从那时起,IPython 将记住该笔记本使用哪个内核。

To use Jupyter notebook with virtual environment (using virtualenvwrapper) plus packages installed in that environment, follow steps below:要将 Jupyter notebook 与虚拟环境(使用 virtualenvwrapper)以及安装在该环境中的软件包一起使用,请按照以下步骤操作:

  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安装 Jupyter 和其他软件包

    pip install jupyter, numpy
  4. Add a new kernel to your Jupyter config将新内核添加到您的 Jupyter 配置中

    ipython kernel install --user --name=your_env_name
  5. Done.完毕。 You may now use Jupyter notebook under the virtual environment.您现在可以在虚拟环境下使用 Jupyter notebook。

     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.所以我只是总结一下,这样有同样问题的人可以很容易地关注。

Assuming that jupyter is installed on your machine, not on the virtual environtment.假设 jupyter 安装在您的机器上,而不是安装在虚拟环境中。

Using a virtual environment with Jupyter notebook在 Jupyter notebook 中使用虚拟环境

VENV_NAME = "YOUR VIRTUAL ENV NAME" VENV_NAME = "你的虚拟环境名称"

1) virtualenv VENV_NAME 1) virtualenv VENV_NAME

2) source venv/bin/activate 2) source venv/bin/activate

3) Add this package if not present: pip3 install ipykernel 3)如果不存在,请添加此包: pip3 install ipykernel

4) Then execute this command: ipython kernel install --user --name=VENV_NAME 4)然后执行这个命令: ipython kernel install --user --name=VENV_NAME

5) Now open up the Jupyter Notebook and in change kernel select VENV_NAME 5) 现在打开 Jupyter Notebook 并在更改内核中选择VENV_NAME

6) To install a new package perform pip3 install <PACKAGE NAME> in your terminal and repeat step 4. 6) 要安装新包,请在终端中执行pip3 install <PACKAGE NAME>并重复步骤 4。

Hope it helps!希望能帮助到你!

Creation of virtualenv with python3 -m venv command使用 python3 -m venv 命令创建 virtualenv

I had the same problem as yours.我和你有同样的问题。 In my case I had created the virtualenv with the command就我而言,我使用命令创建了 virtualenv

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").问题是我无法在虚拟环境中安装 jupyter,因为它已经在 system-site-package 中(当您尝试安装它时,它会告诉您“要求已经满足”)。

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 :要安装 jupyter,(在第一个实例中 pip,使用此命令不会在您的虚拟环境中安装)但仍然可以访问 system-site-package,您可以运行:

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.激活您的虚拟环境,运行pip3 install jupyter (和pip3 install pip ),然后打开文件 ./my_virtual_env/pyvenv.cfg 中的 include-system-site-packages 选项。

After deactivation and reactivation of you environment, you will have access to system site-packages.在您的环境停用和重新激活后,您将可以访问系统站点包。

Creation of virtualenv with virtualenv command使用 virtualenv 命令创建 virtualenv

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.鉴于此答案,您可以通过创建文件 ./my_virtual_env/lib/python3.4/no-global-site-packages.txt 来阻止对系统站点包的访问,并通过删除它来恢复访问权限。

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激活您的环境并在其中安装 jupyter
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.在安装 jupyter 之前,您必须确保的一件事是您没有安装以下软件包。

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 .如果您以前安装过它们,请先通过pip uninstall卸载它们。

  1. Install your desired packages in activated virtualenv and launch jupyter in it and voila!在激活的 virtualenv 中安装所需的软件包并在其中启动 jupyter,瞧!

You can still install jupyter inside your virtual-environment if you have created your virtual env using:如果您使用以下方法创建了虚拟环境,您仍然可以在虚拟环境中安装 jupyter:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM