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

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

问题描述

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

>which -a 蟒蛇/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 Notebook 时,出现内核错误:

文件/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho‌n3.5/subprocess.py",第 947 行,在 init restore_signals、start_new_session 中)文件/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho‌n3.5/subprocess.py",第 1551 行,在 _execute_child 中引发 child_exception_type(errno_num, err_msg)FileNotFoundError: [错误 2]没有这样的文件或目录:'/Users/npr1/anaconda/envs/py27/bin/python'

我该怎么办?!

解决方案

这很容易解决,但需要理解三个不同的概念:

  1. Unix/Linux/OSX 如何使用 $PATH 来查找可执行文件(Windows 中的 %PATH%)
  2. Python 如何安装和查找包
  3. Jupyter 如何知道要使用什么 Python

为了完整起见,我将尝试对每一个进行快速 ELI5,以便您知道如何以最适合您的方式解决此问题.

1.Unix/Linux/OSX $PATH

当您在提示符下键入任何命令(例如,python)时,系统将有一个明确定义的位置序列,用于查找可执行文件.该序列在名为 PATH 的系统变量中定义,用户可以指定该变量.要查看您的 PATH,您可以输入 echo $PATH.

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

$ 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/ 中的 code>ipython.

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

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

2.Python 如何查找包

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

$ python>>>导入系统>>>系统路径['','/Users/jakevdp/anaconda/lib/python3.5','/Users/jakevdp/anaconda/lib/python3.5/site-packages',...]

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

需要注意的重要一点是每个 python 安装都有自己的站点包,其中安装了针对特定 Python 版本的包.换句话说,如果你安装了一些东西,例如/usr/bin/python,然后 ~/anaconda/bin/python 不能使用那个包,因为它安装在不同的Python!这就是为什么在我们的 twitter 交流中,我建议您专注于一个 Python 安装,并修复您的$PATH,以便您只使用您想使用的那个.

还有另外一个组成部分:一些 Python 包捆绑了可以从命令行运行的独立脚本(例如 pipipythonjupyterpep8 等)默认情况下,这些可执行文件将放置在与用于安装它们的 Python 相同的目录路径中,并且被设计为仅适用于该 Python 安装.

这意味着,当你的系统设置好时,当你运行 python 时,你会得到 /usr/bin/python,但是当你运行 ipython,您将获得 /Library/Frameworks/Python.framework/Versions/3.5/bin/ipython,它与 /Library/Frameworks/Python.framework 中的 Python 版本相关联/Versions/3.5/bin/python!此外,这意味着您在运行 python 时可以导入的包与您在运行 ipython 或 Jupyter notebook 时可以导入的包完全分开:您完全使用了两个独立的 Python 安装.

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

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

我们还没有完全摆脱困境.您提到在 Jupyter 笔记本中,您收到内核错误:这表明 Jupyter 正在寻找不存在的 Python 版本.

Jupyter 被设置为能够使用广泛的内核"或代码执行引擎.这些可以是 Python 2、Python 3、R、Julia、Ruby……有许多可能的内核可供使用.但是为了实现这一点,Jupyter 需要知道在哪里寻找相关的可执行文件:也就是说,它需要知道python所在的路径.>

这些路径在 jupyter 的 kernelspec 中指定,用户可以根据自己的需要调整它们.例如,这是我系统上的内核列表:

$ jupyter kernelspec 列表可用内核:python2.7/Users/jakevdp/.ipython/kernels/python2.7python3.3/Users/jakevdp/.ipython/kernels/python3.3python3.4/Users/jakevdp/.ipython/kernels/python3.4python3.5/Users/jakevdp/.ipython/kernels/python3.5python2/Users/jakevdp/Library/Jupyter/kernels/python2python3/Users/jakevdp/Library/Jupyter/kernels/python3

其中每一个都是一个目录,其中包含一些元数据,这些元数据指定了内核名称、可执行文件的路径以及其他相关信息.
您可以手动调整内核,编辑上面列出的目录中的元数据.

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

$ python -m ipykernel install

它将创建一个与用于运行此命令的 Python 可执行文件相关联的内核规范.然后,您可以在 Jupyter notebook 中选择该内核,以使用该 Python 运行您的代码.

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

$ python -m ipykernel install --help用法:ipython-kernel-install [-h] [--user] [--name NAME][--display-name DISPLAY_NAME] [--prefix PREFIX][--sys-前缀]安装 IPython 内核规范.可选参数:-h, --help 显示此帮助信息并退出--user 为当前用户而不是系统范围安装--name NAME 指定内核规范的名称.这需要同时拥有多个 IPython 内核.--display-name DISPLAY_NAME指定内核规范的显示名称.这是当您有多个 IPython 内核时很有帮助.--prefix PREFIX 指定内核规范的安装前缀.这是需要安装到非默认位置,例如一个 conda/virtual-env.--sys-prefix 安装到 Python 的 sys.prefix.简写--prefix='/Users/bussonniermatthias/anaconda'.用来在 conda/virtual-envs 中.

注意:最新版本的 anaconda 附带了一个笔记本扩展,如果安装了 ipykernel 包,它应该会自动检测您的各种 conda 环境.

总结:解决您的问题

因此,在这种背景下,您的问题很容易解决:

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

  2. 确保为那个 python 安装了您要使用的包.如果您使用的是 conda,则可以键入,例如conda install jupyter matplotlib scikit-learnanaconda/bin/python 安装这些包.

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

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

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

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

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' 

What should I do?!

解决方案

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

  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

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.

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.

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/

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

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 - How to set environment variables in Windows 10

2. How Python finds packages

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',
 ...]

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.

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.

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.

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.

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.

3. How Jupyter knows what Python to use

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

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.

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

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.

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.

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.

Wrap-up: Fixing your Issue

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

  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.

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

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

  4. 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天全站免登陆