【问题标题】:How to make iPython use Python 2 instead of Python 3如何让 iPython 使用 Python 2 而不是 Python 3
【发布时间】:2017-03-18 02:33:34
【问题描述】:

我同时安装了 Python 2.7 和 3.5。如果我使用 python 从命令行运行脚本,它使用 Python 2.7,但如果我启动 iPython,它使用 Python 3:

kurt@kurt-ThinkPad:~$ python -V
Python 2.7.12
kurt@kurt-ThinkPad:~$ ipython
Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 

有没有办法启动 iPython 以便它使用 Python 2? (我使用的是 Ubuntu LTS 16.04)。

【问题讨论】:

  • 你是如何安装 ipython 的?
  • 我仍在寻找解决方案,因为虚拟环境无法访问未使用pip 安装的其他模块,例如rethinkdbNurzhan,我不记得我是如何安装 iPython 的,但我相信我下载了 .whl 文件并做了 pip install [ipython_file].whl
  • 我使用 sudo apt-get install ipython 在 xubuntu 16.04 LTS 上安装了 ipython。它默认使用 python 2.7.12。我不知道你为什么有这个问题。我知道从 Ubuntu 16.04 LTS 开始的 vim 默认使用 python3。

标签: python-2.7 ipython


【解决方案1】:

我通过py选择执行的python版本,像这样:

py -2.7 -m IPython

2.7 是我需要的版本。

【讨论】:

    【解决方案2】:

    对这个问题的侵入性较小的解决方案(因为我下面的解决方案不需要更改任何库文件)是

    python2.7 -m IPython notebook
    

    所以一般的命令是

    {{python-you-want-ipython-to-use}} -m IPython 笔记本

    为什么会这样?

    因为如果你看到ipython script (/usr/local/bin/ipython) 它本身似乎是一个 python 脚本并且它有 shebang (#!/usr/bin/python3),所以 ipython 是不是一个独立的二进制文件,但由于一些 python,它得到了生命。因为 ipython 脚本本身需要一些 python 来运行它,所以你直接使用你选择的一些 python 运行 ipython 模块,而不是让 /usr/local/bin/ipython 为你决定它,这就是修复'python ipython 使用什么'的问题。

    【讨论】:

    • pythonX -m IPython 对我来说已经足够了。
    【解决方案3】:

    现在 IPython 6.0+ 不支持 Python 2.6、2.7、3.0、3.1 或 3.2。 使用 Python 2.7 时,请安装 IPython 5.x LTS Long Term Support 版本。

    从 IPython 6.0 开始,需要 Python 3.3 及以上版本。

    【讨论】:

      【解决方案4】:

      跟随ipython reads wrong python version,在/usr/local/bin/ipython,我只是改变了

      #!/usr/bin/python3
      

      在第一行到

      #!/usr/bin/python
      

      Python 2 已经成为 iPython 使用的默认版本:

      kurt@kurt-ThinkPad:~$ ipython
      Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
      Type "copyright", "credits" or "license" for more information.
      
      IPython 2.4.1 -- An enhanced Interactive Python.
      ?         -> Introduction and overview of IPython's features.
      %quickref -> Quick reference.
      help      -> Python's own help system.
      object?   -> Details about 'object', use 'object??' for extra details.
      

      【讨论】:

      • 我更改了该行并收到错误消息“ImportError: No module named IPython”
      【解决方案5】:

      Using both Python 2.x and Python 3.x in IPython Notebookcel 的第二个解决方案(针对非 Anaconda 用户)之后,我为 Python 2 和 Python 3 设置了两个虚拟环境,并分别在每个环境上安装了 iPython。

      【讨论】:

        猜你喜欢
        • 2015-11-01
        • 2014-07-18
        • 1970-01-01
        • 2019-10-04
        • 1970-01-01
        • 1970-01-01
        • 2017-10-03
        • 2013-12-20
        • 2018-10-18
        相关资源
        最近更新 更多