使用多个Python和IPython路径运行Jupyter [英] Running Jupyter with multiple Python and IPython paths

查看:71
本文介绍了使用多个Python和IPython路径运行Jupyter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Jupyter笔记本,但是在进行基本导入时遇到了困难(例如导入matplotlib)。我想这是因为我有几个用户管理的python安装。例如:

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:

> 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

我曾经有过anaconda,但是从〜/ anaconda目录中删除了。现在,当我启动一个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:

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' 

我该怎么办?!

推荐答案

这很容易解决,但它涉及到三个不同的概念:

This is fairly straightforward to fix, but it involves understanding three different concepts:


  1. Unix / Linux / OSX如何使用 $ PATH 查找可执行文件(%PATH% in Windows)

  2. Python如何安装和查找软件包

  3. Jupyter如何知道使用什么Python

  1. How Unix/Linux/OSX use $PATH to find executables (%PATH% in Windows)
  2. How Python installs and finds packages
  3. How Jupyter knows what Python to use

为了这个目的完整性,我会尝试在每个上面快速完成ELI5,这样你就会知道如何以最好的方式解决这个问题。

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.

当你在提示符下键入任何命令时(例如, python ),系统有一个明确定义的位置序列,它可以查找可执行文件。此序列在名为 PATH 的系统变量中定义,用户可以指定。要查看 PATH ,您可以输入 echo $ 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.

结果是计算机上的目录列表,将按按顺序搜索所需的可执行文件。从上面的输出中,我假设它包含:

The result is a list of directories on your computer, which will be searched in order for 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/

在Windows中 echo%path%

可能还有一些其他路径也散布在一起。这意味着当您键入 python 时,系统将转到 / usr / bin / python 。当您键入 ipython 时,在此示例中,系统将转到 /Library/Frameworks/Python.framework/Versions/3.5/bin/ipython ,因为 / usr / bin / 中没有 ipython

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/.

了解您正在使用的可执行文件始终很重要,尤其是当您在系统上安装了相同程序的许多文件时。改变路径并不太复杂;见例如如何在Linux上永久设置$ PATH?

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?.

Windows - 如何在Windows 10中设置环境变量

Windows - How to set environment variables in Windows 10

当您运行python并执行类似 import matplotlib 之类的操作时,Python必须玩类似的游戏找到你想要的包裹。与unix中的 $ PATH 类似,Python有 sys.path 指定这些:

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 sys
>>> sys.path
['',
 '/Users/jakevdp/anaconda/lib/python3.5', 
 '/Users/jakevdp/anaconda/lib/python3.5/site-packages',
 ...]

一些重要的事情:默认情况下,第一个条目in sys.path 是当前目录。另外,除非你修改它(除非你确切知道你在做什么,否则你不应该这样做),你通常会在路径中找到一个名为 site-packages 的东西:这是Python在使用 python setup.py install pip 安装软件包时放置软件包的默认位置,或者 conda 或类似方法。

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.

需要注意的重要一点是每个python安装都有它的自己的站点包,其中包安装用于特定的Python版本。换句话说,如果您安装了某些内容,例如 / usr / bin / python ,然后〜/ anaconda / bin / python 无法使用该包,因为它安装在不同的Python上!这就是为什么在我们的Twitter交换中我建议你专注于一个Python安装,并修复你的 $ PATH ,这样你就只能使用你想要使用的那个。

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软件包捆绑了可以从命令行运行的独立脚本(示例是 pip ipython jupyter pep8 ,等等。)默认情况下,这些可执行文件将放在与用于安装它们的Python相同的相同的目录路径中,并且只能用于使用该Python安装

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 path as the Python used to install them, and are designed to work only with that Python installation.

这意味着,在您的系统设置时,当您运行 python 时,您将获得 / usr / bin / python ,但是当你运行 ipython 时,你会得到 / Library / Frameworks / Python。 framework / Versions / 3.5 / bin / ipython /Library/Frameworks/Python.framework/Versions/3.5/bin/python !此外,这意味着您在运行 python 时可以导入的包与运行 ipython 或者是一个Jupyter笔记本:你正在使用两个完全独立的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.

那么如何解决这个问题呢?好吧,首先确保你的 $ PATH 变量正在按你的意愿行事。您可能有一个名为〜/ .bash_profile 〜/ .bashrc 的启动脚本设置此 $ PATH 变量。在Windows上,您可以修改特定于用户的环境变量。如果您希望系统以不同的顺序搜索事物,您可以手动修改它。当你第一次安装anaconda / miniconda时,会有一个选项自动执行此操作(将Python添加到PATH中):对此说是,然后 python 将始终指向〜/ anaconda / 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.

我们还没有完全脱离水面。你提到在Jupyter笔记本中,你得到一个内核错误:这表明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已经设置好了能够使用各种内核或代码的执行引擎。这些可以是Python 2,Python 3,R,Julia,Ruby ......有许多可能的内核可供使用。但为了实现这一点,Jupyter需要知道 where 来查找相关的可执行文件:也就是说,它需要知道 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 where to look for the associated executable: that is, it needs to know which path the python sits in.

这些路径在jupyter的 kernelspec 中指定,用户可以将它们调整为他们的欲望。例如,这是我系统上的内核列表:

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 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.

安装内核的命令可能会因内核而异。 IPython依赖于 ipykernel软件包,其中包含安装python内核的命令:例如

The command to install a kernel can change depending on the kernel. IPython relies on the ipykernel package which contains a command to install a python kernel: for example

$  python -m ipykernel install

它将创建与用于运行此命令的Python可执行文件关联的kernelspec。然后,您可以在Jupyter笔记本中选择此内核以使用该Python运行代码。

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.

您可以使用help命令查看ipykernel提供的其他选项:

You can see other options that ipykernel provides using the help command:

$ 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.

注意:最新版本的 anaconda 附带笔记本电脑的扩展名如果在其中安装了 ipykernel 包,它应自动检测您的各种conda环境。

Note: the recent version of anaconda ships with an extension for the notebook that should automatically detect your various conda environments if the ipykernel package is installed in it.

所以在这样的背景下,你的问题很容易解决:

So with that background, your issue is quite easy to fix:


  1. 设置 PATH ,以便首先获得所需的Python版本。例如,您可以运行 export PATH =/ path / to / python / bin:$ PATH来指定(一次)您要使用的Python。要永久执行此操作,请将该行添加到 .bash_profile / .bashrc (请注意,anaconda可以自动执行此操作当你安装它时)。我建议使用anaconda或miniconda附带的Python:这将允许你 conda install 你需要的所有工具。

  1. Set your PATH so that the desired Python version is first. For example, you could run export 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 to conda install all the tools you need.

确保为那个 python安装了要使用的软件包。如果你正在使用conda,你可以输入,例如 conda install jupyter matplotlib scikit-learn anaconda / bin / python 安装这些软件包。

Make sure the packages you want to use are installed for that python. If you're using conda, you can type, e.g. conda install jupyter matplotlib scikit-learn to install those packages for anaconda/bin/python.

确保您的Jupyter内核指向您要使用的Python版本。当你 conda install jupyter 时,它应该自动为 anaconda / bin / python 设置它。否则,您可以使用 jupyter kernelspec 命令或 python -m ipykernel安装命令来调整现有内核或安装新内核。

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 for anaconda/bin/python automatically. Otherwise you can use the jupyter kernelspec command or python -m ipykernel install command to adjust existing kernels or install new ones.

要将模块安装到不受Anaconda管理的其他Python Jupyter内核中,您需要复制内核的Python可执行文件的路径并运行 / path / to / python -m pip install< package>

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>

希望这很清楚......祝你好运!

Hopefully that's clear... good luck!

这篇关于使用多个Python和IPython路径运行Jupyter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆