ipython reads wrong python version

I've been having trouble with Python, iPython and the libraries. The following points show the chain of the problematics. I'm running Python 2.7 on Mac Lion.

  1. iPython doesn't read the libraries of scipy, matplotlib, but it does read numpy.
  2. To fix this, I tried installing Python's source code version, and it only gave me more problems since now I have two different versions: 2.7.1 and 2.7.2
  3. I noticed that running Python, uses version 2.7.2 and does import scipy, matplotlib, and numpy, but on iPython the version is 2.7.1 which doesn't open scipy or matplotlib.

I've tried several things that I've encountered from other blogposts. But none of them have helped, and also unfortunately I don't quite know what I'm doing with some of them. For example: I tried uninstalling and reinstalling ipython with easy_install and pip. I also tried reinstalling everything through homebrew, and modifying the path .bash_profile.

101220 次浏览

First, I would make sure you're using the right python. At a command prompt type:

which python
python -V

第一个将告诉您路径,第二个将告诉您正在使用的 Python 版本。

好吧,快速解决:

which python

gives you /usr/bin/python, right? Do

which ipython

and I bet that'll be /usr/local/bin/ipython. Let's look inside:

编辑9/7/16——现在的文件如下:

cat /usr/local/bin/ipython


#!/usr/bin/python


# -*- coding: utf-8 -*-
import re
import sys


from IPython import start_ipython


if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start_ipython())

我的工作就是这样,但我的情况不完全像观察所的。


原始答案——9/30/13:

cat /usr/local/bin/ipython


#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'ipython==0.12.1','console_scripts','ipython'
__requires__ = 'ipython==0.12.1'
import sys
from pkg_resources import load_entry_point


if __name__ == '__main__':
sys.exit(
load_entry_point('ipython==0.12.1', 'console_scripts', 'ipython')()
)

啊哈——在编辑器中打开 /usr/local/bin/ipython(具有特权) ,并将第一行更改为

#!/usr/local/bin/python

保存,启动 iPython,应该说它正在使用你想要的版本。

使用虚拟世界怎么样? 我真的很喜欢它。也许这不是更快的方式,但我认为它是非常清楚的。

When you create a virtualenv, you can specify the python path with the -p flag.

对于 python 2.7

$ virtualenv -p /usr/bin/python2.7 venv2.7
$ source venv2.7/bin/activate
(venv2.7)$ pip install ipython
(venv2.7)$ ipython

对于 python 3.4

$ virtualenv -p /usr/bin/python3.4 venv3.4
$ source venv3.4/bin/activate
(venv3.4)$ pip install ipython
(venv3.4)$ ipython

使用 Penenv的类似方法

pyenv install 3.4.5
pyenv local 3.4.5
pip install ipython
ipython

现在它将显示 Python 的正确版本

Python 3.4.5

极其相关: http://conda.pydata.org/docs/troubleshooting.html#shell-command-location

Lr 问题是由于 shell 的“哈希”和路径变量引起的。

我能想到的绝对最简单的解决方案,不需要修改环境、安装的文件或其他任何东西,依赖于以下事实

  1. 可执行 ipython实际上是一个 Python 脚本。
  2. The IPython package is installed separately for each interpreter that you ran pip intall with.

If the version of Python you are runninig with has an IPython package installed, you can just do

/path/to/desired/python $(which ipython)

这将使用您想要的解释器而不是 shebang 中列出的解释器运行 ipython脚本。

您的问题基本上是让 ipython 使用正确的 python。

所以这个问题的解决方案是让 ipython 使用正确的 python (它安装了类似 spypy 的库)

我在这里写了一个解决方案:

如何让 iPython 使用 Python2而不是 Python3

我遇到了同样的问题,但以下是唯一的解决方案,什么为我的 OSX12,塞拉利昂工作。

Ipython 总是针对 python 3.6发布的,但是我需要它来支持2.7。我找不到2.7版本的 IPython 启动脚本,也找不到用 python -m执行的 IPython 模块。没有 brew instally ipython pip install ipythonpip2 install ipython可以得到我的2.7版本。所以我手动操作。

brew install ipython@5给你安装2.7版本,但不会将其放在 $PATH上,因为它知道名称与另一个包冲突。 ln -s /usr/local/Cellar/ipython@5/5.5.0_1/bin/ipython /usr/local/bin/ipython2将解决这个问题,并允许您从 shell 提示符运行 ipython2

For me, because I was serious about using ipython for 2.7, I also ran the following commands.

ln -s /usr/local/Cellar/ipython/6.2.1/bin/ipython /usr/local/bin/ipython3
rm -f /usr/local/bin/ipython
ln -s /usr/local/bin/ipython2 /usr/local/bin/ipython

我的解决办法很简单,很蠢,但是很有效。

我使用 python -V来确定什么是版本

$ python -V
Python 2.7.10

然后在. bash _ profile 中创建别名

$ vi ~/.bash_profile

加一条线

alias ipython="python -m IPython"

然后在 python 2.7中得到一个 ipython

(顺便说一下,我的 ipython是通过 homebrew安装的,它默认会在 python3中运行 ipython。)

$ brew install ipython

如果您正在使用水蟒或其他虚拟环境包装器,那么这里提到的所有答案都无助于解决这个问题。

这个答案是基于假设您正在使用蟒蛇。

假设您处于 python3环境中,并且在 jupyter 笔记本上创建笔记本时,它显示的是“ Python2”而不是“ Python3”。

这是因为“ ipython”本质上是一个正在运行的脚本,并且在这个脚本中它提到了它正在使用哪个 python 版本来执行命令。您所需要做的就是为 ipython 更改这一行,以使用所需的 python 版本。

First stop the ipython server and get the location of the python executable of the current environment using the command "which python"

我的输出是:

/home/sourabh/anaconda2/envs/py3/bin/python

现在使用命令“ which ipython”获取 ipython 的可执行位置

我的是:

/home/sourabh/anaconda2/envs/py2/bin/python

注意,它使用的是 pythonie 的不同版本。来自特定环境的 python 运行不同版本的 python 即从不同的环境运行 python。

Now navigate to the directory anaconda2/bin(for anaconda 3 users it should be anaconda3/bin ) and search for "ipython" file. in this edit the first line to be point it to the current python version which you want. ie the output of "which python" i.e. :

#!/home/sourabh/anaconda2/envs/py3/bin/python

注意,我将 python 环境从 py2(运行 python 2.7)更改为 py3(运行 python 3.5)。

Save the file. And run jupyter notebook, now when creating a new notebook the "Python 3" option should be visible.

干杯!

把@Matt 的评论作为一个答案贴出来,这样就更容易看到了

python -m IPython

首先用路径上可访问的 python 作为模块加载 ipython。在我的情况下,我有一个预装和一个我添加从酿造。这真是太完美了。

For me, on windows 10, my Ipython exe did not match my 巨蟒. I knew this because ipython said

----> 3 from pywinauto import handleprops
4 from pywinauto import timings
5 from pywinauto.application import Application


ModuleNotFoundError: No module named 'pywinauto'

However, this worked when I executed the containing program with > python

这是不匹配的。

Ipython info

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.


[DOT DOT DOT]

python info

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

这是我的解决办法。 我更改了对 Python\...\Scripts的引用,这就是 ipython.exe 隐藏的地方

  1. I变了我的系统 PATH 在设置-环境变量
  • was C:\Users\joeco\AppData\Roaming\Python\Python38\Scripts
  • 现在是 C:\Users\joeco\AppData\Roaming\Python\Python39\Scripts\
  1. 将上面的值 起来移动到路径的顶部
  2. 重启

结果

  1. Ipython 现在匹配 python
  2. Ipython from pywinauto import handleprops成功

我安装了 python2.7、 python3.6和 python3.8

sahil@rubeus:~$ ls -ls /usr/bin/python*
0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python -> python2.7
0 lrwxrwxrwx 1 root root       9 Apr 16  2018 /usr/bin/python2 -> python2.7
3548 -rwxr-xr-x 1 root root 3633000 Feb 27 20:40 /usr/bin/python2.7
0 lrwxrwxrwx 1 root root       9 Oct 25  2018 /usr/bin/python3 -> python3.6
4424 -rwxr-xr-x 2 root root 4526456 Jan 26 21:03 /usr/bin/python3.6
5016 -rwxr-xr-x 1 root root 5134032 Jun  3 23:23 /usr/bin/python3.8


我把 ipython 文件放在这个位置

sahil@rubeus:~$ which ipython
/home/sahil/.local/bin/ipython

它有下面的内容

#!/usr/bin/python2


# -*- coding: utf-8 -*-
import re
import sys


from IPython import start_ipython


if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(start_ipython())

因此,我只是在同一位置创建了两个名为 ipython3.6和 ipython3.8的文件副本,并更新了第一行以指向特定的 python 版本,在我的例子中是 /usr/bin/python3.6/usr/bin/python3.8

所以现在有3个文件,每个文件都指向特定的 python 版本

sahil@rubeus:~$ ls ~/.local/bin/ipython*
/home/sahil/.local/bin/ipython
/home/sahil/.local/bin/ipython3.6
/home/sahil/.local/bin/ipython3.8

Now when I need to start python2 I just run ipython from terminal and same way to get ipython in python3.8, I run ipython3.8

sahil@rubeus:~$ ipython3.8
Python 3.8.11 (default, Jul  3 2021, 17:53:42)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.


In [1]:

注意: 您可能需要使用命令 python3.6 -m pip install IPythonpython3.8 -m pip install IPython为特定的 python 版本安装 ipython。我不确定它是安装了 python 还是我单独安装的。