使用多个 Python 和 IPython 路径运行 Jupyter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39007571/
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 with multiple Python and IPython paths
提问by npross
I'd like to work with Jupyter notebooks, but have had difficulty doing basic imports (such as import matplotlib). I think this was because I have several user-managed python installations. For instance:
我想使用 Jupyter 笔记本,但在进行基本导入(例如导入 matplotlib)时遇到困难。我认为这是因为我有几个用户管理的 python 安装。例如:
> which -a python
/usr/bin/python
/usr/local/bin/python
> which -a ipython
/Library/Frameworks/Python.framework/Versions/3.5/bin/ipython
/usr/local/bin/ipython
> which -a jupyter
/Library/Frameworks/Python.framework/Versions/3.5/bin/jupyter
/usr/local/bin/jupyter
I used to have anaconda, but removed if from the ~/anaconda directory. Now, when I start a Jupyter Notebook, I get a Kernel Error:
我曾经有 anaconda,但如果从 ~/anaconda 目录中删除。现在,当我启动 Jupyter Notebook 时,出现内核错误:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho?n3.5/subprocess.py",
line 947, in init restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho?n3.5/subprocess.py",
line 1551, in _execute_child raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2]
No such file or directory: '/Users/npr1/anaconda/envs/py27/bin/python'
What should I do?!
我该怎么办?!
回答by jakevdp
This is fairly straightforward to fix, but it involves understanding three different concepts:
这很容易解决,但它涉及理解三个不同的概念:
- How Unix/Linux/OSX use
$PATH
to find executables (%PATH%
in Windows) - How Python installs and finds packages
- How Jupyter knows what Python to use
- Unix/Linux/OSX 如何使用
$PATH
查找可执行文件(%PATH%
在 Windows 中) - Python 如何安装和查找包
- Jupyter 如何知道要使用什么 Python
For the sake of completeness, I'll try to do a quick ELI5 on each of these, so you'll know how to solve this issue in the best way for you.
为完整起见,我将尝试对其中的每一个进行快速 ELI5,以便您知道如何以最适合您的方式解决此问题。
1. Unix/Linux/OSX $PATH
1. Unix/Linux/OSX $PATH
When you type any command at the prompt (say, python
), the system has a well-defined sequence of places that it looks for the executable. This sequence is defined in a system variable called PATH
, which the user can specify. To see your PATH
, you can type echo $PATH
.
当您在提示符下键入任何命令时(例如,python
),系统有一个明确定义的位置序列,用于查找可执行文件。该序列在名为 的系统变量中定义,PATH
用户可以指定该变量。要查看您的PATH
,您可以输入echo $PATH
.
The result is a list of directories on your computer, which will be searched in orderfor the desired executable. From your output above, I assume that it contains this:
结果是您计算机上的目录列表,将按顺序搜索所需的可执行文件。从上面的输出中,我假设它包含以下内容:
$ echo $PATH
/usr/bin/:/Library/Frameworks/Python.framework/Versions/3.5/bin/:/usr/local/bin/
In windows echo %path%
在窗口中 echo %path%
Probably with some other paths interspersed as well. What this means is that when you type python
, the system will go to /usr/bin/python
. When you type ipython
, in this example, the system will go to /Library/Frameworks/Python.framework/Versions/3.5/bin/ipython
, because there is no ipython
in /usr/bin/
.
可能还穿插了一些其他路径。这意味着当您键入时python
,系统将转到/usr/bin/python
。当您键入 时ipython
,在本例中,系统将转到/Library/Frameworks/Python.framework/Versions/3.5/bin/ipython
,因为没有ipython
in /usr/bin/
。
It's always important to know what executable you're using, particularly when you have so many installations of the same program on your system. Changing the path is not too complicated; see e.g. How to permanently set $PATH on Linux?.
了解您正在使用的可执行文件总是很重要的,尤其是当您的系统上安装了如此多的相同程序时。改变路径并不太复杂;参见例如如何在 Linux 上永久设置 $PATH?.
Windows - How to set environment variables in Windows 10
Windows -如何在 Windows 10 中设置环境变量
2. How Python finds packages
2. Python 如何查找包
When you run python and do something like import matplotlib
, Python has to play a similar game to find the package you have in mind. Similar to $PATH
in unix, Python has sys.path
that specifies these:
当你运行 python 并做类似的事情时import matplotlib
,Python 必须玩一个类似的游戏来找到你心目中的包。与$PATH
在 unix 中类似,Pythonsys.path
指定了这些:
$ python
>>> import sys
>>> sys.path
['',
'/Users/jakevdp/anaconda/lib/python3.5',
'/Users/jakevdp/anaconda/lib/python3.5/site-packages',
...]
Some important things: by default, the first entry in sys.path
is the current directory. Also, unless you modify this (which you shouldn't do unless you know exactly what you're doing) you'll usually find something called site-packages
in the path: this is the default place that Python puts packages when you install them using python setup.py install
, or pip
, or conda
, or a similar means.
一些重要的事情:默认情况下,第一个条目sys.path
是当前目录。此外,除非你修改它(除非你确切地知道你在做什么,否则你不应该这样做)你通常会site-packages
在路径中找到一些被调用的东西:这是当你使用安装包时Python放置包的默认位置python setup.py install
,或者pip
, 或conda
, 或类似的方法。
The important thing to note is that each python installation has its own site-packages, where packages are installed for that specific Python version. In other words, if you install something for, e.g. /usr/bin/python
, then ~/anaconda/bin/python
can't use that package, because it was installed on a different Python! This is why in our twitter exchange I recommended you focus on one Python installation, and fix your$PATH
so that you're only using the one you want to use.
需要注意的重要一点是,每个 python 安装都有自己的 site-packages,其中安装了针对特定 Python 版本的包。换句话说,如果你为 eg 安装了一些东西/usr/bin/python
,那么~/anaconda/bin/python
就不能使用那个包,因为它是安装在不同的 Python 上的!这就是为什么在我们的 twitter 交流中,我建议你专注于一个 Python 安装,并修复你的,$PATH
以便你只使用你想使用的那个。
There's another component to this: some Python packages come bundled with stand-alone scripts that you can run from the command line (examples are pip
, ipython
, jupyter
, pep8
, etc.) By default, these executables will be put in the same directory pathas the Python used to install them, and are designed to work only with that Python installation.
有此另一个组件:一些Python包都带有独立的脚本捆绑在一起,你可以在命令行(例子是运行pip
,ipython
,jupyter
,pep8
,等)默认情况下,这些可执行文件将被放在同一目录路径为Python的用于安装它们,并且设计为仅适用于该 Python 安装。
That means that, as your system is set-up, when you run python
, you get /usr/bin/python
, but when you run ipython
, you get /Library/Frameworks/Python.framework/Versions/3.5/bin/ipython
which is associated with the Python version at /Library/Frameworks/Python.framework/Versions/3.5/bin/python
! Further, this means that the packages you can import when running python
are entirely separate from the packages you can import when running ipython
or a Jupyter notebook: you're using two completely independent Python installations.
这意味着,随着您的系统的设置,当您运行时python
,您会得到/usr/bin/python
,但是当您运行时ipython
,您会得到/Library/Frameworks/Python.framework/Versions/3.5/bin/ipython
与 Python 版本相关联的/Library/Frameworks/Python.framework/Versions/3.5/bin/python
! 此外,这意味着您在运行时可以导入的包与您在运行时可以导入python
的包ipython
或 Jupyter 笔记本完全分开:您正在使用两个完全独立的 Python 安装。
So how to fix this? Well, first make sure your $PATH
variable is doing what you want it to. You likely have a startup script called something like ~/.bash_profile
or ~/.bashrc
that sets this $PATH
variable. On Windows, you can modify the user specific environment variables. You can manually modify that if you want your system to search things in a different order. When you first install anaconda/miniconda, there will be an option to do this automatically (add Python to the PATH): say yes to that, and then python
will always point to ~/anaconda/python
, which is probably what you want.
那么如何解决这个问题呢?好吧,首先确保您的$PATH
变量正在执行您想要的操作。您可能有一个名为类似~/.bash_profile
或~/.bashrc
设置此$PATH
变量的启动脚本。在 Windows 上,您可以修改用户特定的环境变量。如果您希望系统以不同的顺序搜索内容,您可以手动修改它。当您第一次安装 anaconda/miniconda 时,将有一个选项可以自动执行此操作(将 Python 添加到 PATH):对此说是,然后python
将始终指向~/anaconda/python
,这可能是您想要的。
3. How Jupyter knows what Python to use
3. Jupyter 如何知道使用什么 Python
We're not totally out of the water yet. You mentioned that in the Jupyter notebook, you're getting a kernel error: this indicates that Jupyter is looking for a non-existent Python version.
我们还没有完全脱离水面。您提到在 Jupyter 笔记本中,您收到内核错误:这表明 Jupyter 正在寻找不存在的 Python 版本。
Jupyter is set-up to be able to use a wide range of "kernels", or execution engines for the code. These can be Python 2, Python 3, R, Julia, Ruby... there are dozens of possible kernels to use. But in order for this to happen, Jupyter needs to know whereto look for the associated executable: that is, it needs to know which path the python
sits in.
Jupyter 被设置为能够使用广泛的“内核”或代码执行引擎。这些可以是 Python 2、Python 3、R、Julia、Ruby……有许多可能的内核可供使用。但是为了实现这一点,Jupyter 需要知道在哪里寻找相关的可执行文件:也就是说,它需要知道python
位于哪个路径。
These paths are specified in jupyter's kernelspec
, and it's possible for the user to adjust them to their desires. For example, here's the list of kernels that I have on my system:
这些路径在 jupyter 中指定,kernelspec
用户可以根据自己的需要调整它们。例如,这是我系统上的内核列表:
$ jupyter kernelspec list
Available kernels:
python2.7 /Users/jakevdp/.ipython/kernels/python2.7
python3.3 /Users/jakevdp/.ipython/kernels/python3.3
python3.4 /Users/jakevdp/.ipython/kernels/python3.4
python3.5 /Users/jakevdp/.ipython/kernels/python3.5
python2 /Users/jakevdp/Library/Jupyter/kernels/python2
python3 /Users/jakevdp/Library/Jupyter/kernels/python3
Each of these is a directory containing some metadata that specifies the kernel name, the path to the executable, and other relevant info.
You can adjust kernels manually, editing the metadata inside the directories listed above.
其中每一个都是一个包含一些元数据的目录,这些元数据指定了内核名称、可执行文件的路径和其他相关信息。
您可以手动调整内核,编辑上面列出的目录中的元数据。
The command to install a kernel can change depending on the kernel. IPython relies on the ipykernel packagewhich contains a command to install a python kernel: for example
安装内核的命令可能因内核而异。IPython 依赖于ipykernel 包,其中包含安装 python 内核的命令:例如
$ python -m ipykernel install
It will create a kernelspec associated with the Python executable you use to run this command. You can then choose this kernel in the Jupyter notebook to run your code with that Python.
它将创建一个与用于运行此命令的 Python 可执行文件相关联的 kernelspec。然后,您可以在 Jupyter Notebook 中选择此内核以使用该 Python 运行您的代码。
You can see other options that ipykernel provides using the help command:
您可以使用 help 命令查看 ipykernel 提供的其他选项:
$ python -m ipykernel install --help
usage: ipython-kernel-install [-h] [--user] [--name NAME]
[--display-name DISPLAY_NAME] [--prefix PREFIX]
[--sys-prefix]
Install the IPython kernel spec.
optional arguments:
-h, --help show this help message and exit
--user Install for the current user instead of system-wide
--name NAME Specify a name for the kernelspec. This is needed to
have multiple IPython kernels at the same time.
--display-name DISPLAY_NAME
Specify the display name for the kernelspec. This is
helpful when you have multiple IPython kernels.
--prefix PREFIX Specify an install prefix for the kernelspec. This is
needed to install into a non-default location, such as
a conda/virtual-env.
--sys-prefix Install to Python's sys.prefix. Shorthand for
--prefix='/Users/bussonniermatthias/anaconda'. For use
in conda/virtual-envs.
Note: the recent version of anacondaships with an extension for the notebook that should automatically detect your various conda environments if the ipykernel
package is installed in it.
注意:最新版本的anaconda附带了笔记本的扩展,如果ipykernel
安装了软件包,它应该会自动检测您的各种 conda 环境。
Wrap-up: Fixing your Issue
总结:解决您的问题
So with that background, your issue is quite easy to fix:
因此,在这种背景下,您的问题很容易解决:
Set your
PATH
so that the desired Python version is first. For example, you could runexport PATH="/path/to/python/bin:$PATH"
to specify (one time) which Python you'd like to use. To do this permanently, add that line to your.bash_profile
/.bashrc
(note that anaconda can do this automatically for you when you install it). I'd recommend using the Python that comes with anaconda or miniconda: this will allow you toconda install
all the tools you need.Make sure the packages you want to use are installed for thatpython. If you're using conda, you can type, e.g.
conda install jupyter matplotlib scikit-learn
to install those packages foranaconda/bin/python
.Make sure that your Jupyter kernels point to the Python versions you want to use. When you
conda install jupyter
it should set this up foranaconda/bin/python
automatically. Otherwise you can use thejupyter kernelspec
command orpython -m ipykernel install
command to adjust existing kernels or install new ones.For installing modules into other Python Jupyter kernels not managed by Anaconda, you need to copy the path to the Python executable for the kernel and run
/path/to/python -m pip install <package>
设置您
PATH
以便所需的 Python 版本是第一个。例如,您可以运行export PATH="/path/to/python/bin:$PATH"
以指定(一次)您想要使用的 Python。要永久执行此操作,请将该行添加到您的.bash_profile
/.bashrc
(请注意,anaconda 可以在您安装时自动为您执行此操作)。我建议使用 anaconda 或 miniconda 附带的 Python:这将允许您使用所需的conda install
所有工具。确保为该python安装了要使用的软件包。如果您使用的是 conda,您可以键入,例如
conda install jupyter matplotlib scikit-learn
为anaconda/bin/python
.确保您的 Jupyter 内核指向您要使用的 Python 版本。当你
conda install jupyter
应该anaconda/bin/python
自动设置它时。否则,您可以使用jupyter kernelspec
命令或python -m ipykernel install
命令来调整现有内核或安装新内核。要将模块安装到其他不受 Anaconda 管理的 Python Jupyter 内核中,您需要将路径复制到内核的 Python 可执行文件并运行
/path/to/python -m pip install <package>
Hopefully that's clear... good luck!
希望这很清楚……祝你好运!