Python 在 virtualenv 中运行 Jupyter notebook:安装的 sklearn 模块不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42449814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running Jupyter notebook in a virtualenv: installed sklearn module not available
提问by Homunculus Reticulli
I have installed a created a virtualenv machinelearnand 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?
回答by ogrisel
You probably have not installed jupyter / IPython in your virtualenv. Try the following:
您可能还没有在您的 virtualenv 中安装 jupyter / IPython。请尝试以下操作:
python -c "import IPython"
and check that the jupyter
command found in your $PATH
is the one from the bin
folder of your venv:
并检查jupyter
在您的命令中找到的命令$PATH
是否来自bin
您的 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:
对于 powershell 控制台中的 Windows 用户,您可以使用以下jupyter
命令检查您的命令$env:Path
是否是Scripts
您 venv 文件夹中的命令:
get-command jupyter
Edit: if this is the problem, just run python -m pip install jupyter
in your venv.
编辑:如果这是问题,请python -m pip install jupyter
在您的 venv 中运行。
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”的内核。
回答by ClimbsRocks
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. Follow the following steps:
在链接断开的情况下复制以下内容:您可以为您的 IPython 笔记本使用 virtualenv。请按照以下步骤操作:
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 will remember which kernel to use for that notebook from then on.
您现在应该能够在 IPython notebook 菜单中看到您的内核:Kernel -> Change kernel 并能够切换到它(您可能需要在它出现在列表中之前刷新页面)。从那时起,IPython 将记住该笔记本使用哪个内核。
回答by C. Feng
To use Jupyter notebook with virtual environment (using virtualenvwrapper) plus packages installed in that environment, follow steps below:
要将 Jupyter notebook 与虚拟环境(使用 virtualenvwrapper)以及安装在该环境中的软件包一起使用,请按照以下步骤操作:
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
创建虚拟环境
mkvirtualenv --no-site-packages --python=/your/python/path your_env_name
激活虚拟环境
workon your_env_name
安装 Jupyter 和其他软件包
pip install jupyter, numpy
将新内核添加到您的 Jupyter 配置中
ipython kernel install --user --name=your_env_name
完毕。您现在可以在虚拟环境下使用 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.
免责声明:该问题已得到回答,但隐藏在其中一个答复中。我用谷歌搜索并花了一些时间找到正确的答案。所以我只是总结一下,这样有同样问题的人可以很容易地关注。
回答by user_3pij
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
- 激活您的环境并在其中安装 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
。
- Install your desired packages in activated virtualenv and launch jupyter in it and voila!
- 在激活的 virtualenv 中安装您想要的软件包并在其中启动 jupyter,瞧!
回答by pujanm
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!
希望能帮助到你!
回答by Tiphaine Champetier
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 answeryou 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 来阻止对系统站点包的访问,并通过删除它来恢复访问权限。
回答by Vijay Kumar
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