Python jupyter notebook 在不同的环境中运行内核
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37891550/
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
jupyter notebook running kernel in different env
提问by Paul Gowder
I've gotten myself into some kind of horrible virtualenv mess. Help?!
我让自己陷入了某种可怕的 virtualenv 混乱。帮助?!
I manage environments with conda
. Until recently, I only had a python2 jupyter notebook kernel, but I decided to drag myself kicking and screaming into the 21st century and installed a python3 kernel; I forget how I did it.
我使用conda
. 直到最近,我只有一个python2 jupyter notebook内核,但我决定拖着自己的脚踢和尖叫进入21世纪,安装了一个python3内核;我忘了我是怎么做到的。
My main (anaconda) python defaults to 2.7.
我的主要(anaconda)python 默认为 2.7。
So here I am, merrily trying to use beautiful soup from inside my shiny new python3 kernel, and I don't seem to be able to do anything to get at whatever environment it's finding packages in. Viz (all from notebook):
所以我在这里,愉快地尝试从我闪亮的新 python3 内核中使用漂亮的汤,我似乎无法做任何事情来获得它在其中找到包的任何环境。 Viz(全部来自笔记本):
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
Ok, fine, I'll install it using shell magic. Right? Right?
好的,好的,我将使用 shell magic 安装它。对?对?
! pip install bs4
--> Collecting bs4
Downloading bs4-0.0.1.tar.gz
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4 in /Users/[MY-USER]/anaconda/lib/python2.7/site-packages (from bs4)
[...]
Successfully built bs4
Installing collected packages: bs4
Successfully installed bs4-0.0.1
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
Oh no. Does it think I'm in a 2.7 env even though I'm running a python3 kernel? That won't do.
不好了。即使我运行的是 python3 内核,它是否认为我处于 2.7 环境中?那不行。
! conda info --envs
--> # conda environments:
#
flaskenv /Users/[MY-USER]/anaconda/envs/flaskenv
mesa /Users/[MY-USER]/anaconda/envs/mesa
py35 /Users/[MY-USER]/anaconda/envs/py35
root * /Users/[MY-USER]/anaconda
Ok, I can fix that. One of those is a 3.5 env.
好的,我可以解决这个问题。其中之一是 3.5 环境。
! source activate py35
--> prepending /Users/[MY-USER]/anaconda/envs/py35/bin to PATH
! conda install beautifulsoup4
--> Fetching package metadata .......
Solving package specifications: ..........
# All requested packages already installed.
# packages in environment at /Users/[MY-USER]/anaconda:
#
beautifulsoup4 4.4.1 py27_0
concerning...
关于...
! pip install bs4
--> Requirement already satisfied (use --upgrade to upgrade): bs4 in /Users/[MY-USER]/anaconda/lib/python2.7/site-packages
more concerning...
更多关于...
from bs4 import BeautifulSoup
-> ImportError: No module named 'bs4'
ARRGH!!! headdeskAm I going to have to kill the kernel in order to fix this (and re-run a bit of work)? Is killing the kernel even going to work? How do I get my jupyter kernel to know what environment it's supposed to be running under?
啊啊啊!!! headdesk我是否必须杀死内核才能解决这个问题(并重新运行一些工作)?杀死内核甚至会起作用吗?如何让我的 jupyter 内核知道它应该在什么环境下运行?
thanks!
谢谢!
回答by tschundler
This is a tricky part of ipython / Jupyter. The set of kernels available are independent of what your virtualenv is when you start jupyter Notebook. The trick is setting up the the ipykernel package in the environment you want to identify itself uniquely to jupyter. From docs on multiple ipykernels,
这是 ipython / Jupyter 的一个棘手部分。可用的内核集与启动 jupyter Notebook 时的 virtualenv 无关。诀窍是在您想要为 jupyter 唯一标识自己的环境中设置 ipykernel 包。来自多个 ipykernels 的文档,
source activate ENVNAME
pip install ipykernel
python -m ipykernel install --user --name ENVNAME --display-name "Python (whatever you want to call it)"
If you only want to have a single Python 3 kernel, from the conda environment, just use python -m ipykernel install --user
and it will reset the default python to the one in the virtualenv.
如果您只想拥有一个 Python 3 内核,那么在 conda 环境中,只需使用python -m ipykernel install --user
它,它就会将默认 python 重置为 virtualenv 中的那个。
And yes, you will need to restart the kernel and re-run the prior steps.
是的,您需要重新启动内核并重新运行之前的步骤。
See Also Using both Python 2.x and Python 3.x in IPython Notebook
回答by Antoine Dusséaux
@tschundler's solution works perfectly if your environment has already been created.
如果您的环境已经创建,@tschundler 的解决方案将完美运行。
If you want to change the default kernel at the creation of your virtual environment and avoid any manual configuration, you just need to add jupyter
at the end of the conda command:
如果要在创建虚拟环境时更改默认内核并避免任何手动配置,只需jupyter
在 conda 命令末尾添加:
conda create --name ENVNAME python=PYTHONVERSION jupyter
conda create --name ENVNAME python=PYTHONVERSION jupyter
The correct kernel will then be used when you use ipython or jupyter notebook.
当您使用 ipython 或 jupyter notebook 时,将使用正确的内核。
回答by markroxor
In my case somehow jupyter wasn't able to 'pick' the virtual environment's python. So I had to edit ~/.local/share/jupyter/kernels/{my_env_name}/kernel.json
and add path to the interpreter
在我的情况下,jupyter 无法“选择”虚拟环境的 python。所以我不得不编辑~/.local/share/jupyter/kernels/{my_env_name}/kernel.json
并添加解释器的路径
in the argv
key.
在argv
关键。
回答by Rakend Dubba
There is also an easy way here
这里也有一个简单的方法
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install tornado==4.5.3
pip install ipykernel==4.8.2
You should now be able to 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 to use for that notebook from then on.
您现在应该能够在 IPython notebook 菜单中看到您的内核:Kernel -> Change kernel 并能够切换到它(您可能需要在它出现在列表中之前刷新页面)。从那时起,IPython 将记住该笔记本使用哪个内核。
This worked for me. source
这对我有用。来源
回答by vishal
pip install --user ipykernel
python -m ipykernel install --user --name=myenv
Output
Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv
and go to above directory open kernel.json
并转到上面的目录打开 kernel.json
{
"argv": [
"/home/user/anaconda3/envs/myenv/bin/python", # path to your virtual environment python
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "myenv",
"language": "python"
}