ipython 读取错误的 python 版本

发布于 2025-01-08 00:12:18 字数 466 浏览 0 评论 0原文

我在使用 Python、iPython 和库时遇到了问题。以下几点显示了问题的链条。我在 Mac Lion 上运行 Python 2.7。

  1. iPython不读取scipy、matplotlib的库,但它读取numpy。
  2. 为了解决这个问题,我尝试安装Python的源代码版本,这只给我带来了更多问题,因为现在我有两个不同的版本:2.7.1和2.7.2
  3. 我注意到运行Python,使用版本2.7.2并且导入scipy, matplotlib 和 numpy,但在 iPython 上版本是 2.7.1,它不会打开 scipy 或 matplotlib。

我尝试了其他博客文章中遇到的一些事情。但他们都没有帮助,而且不幸的是我不太知道我在用他们中的一些人做什么。例如: 我尝试使用 easy_install 和 pip 卸载并重新安装 ipython。我还尝试通过自制软件重新安装所有内容,并修改路径.bash_profile。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

┊风居住的梦幻卍 2025-01-15 00:12:18

好的快速修复:

which python

给你 /usr/bin/python,对吗? 这样做

which ipython

,我打赌那将是 /usr/local/bin/ipython。让我们看看里面:

编辑 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)?

我的工作正常,就像这样,但我的情况与OP不完全一样。


原始答案 -- 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,应该说它正在使用你现在想要的版本。

, '', sys.argv[0]) sys.exit(start_ipython())

我的工作正常,就像这样,但我的情况与OP不完全一样。


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

啊哈 - 在编辑器中打开 /usr/local/bin/ipython (具有权限),然后更改第一行以

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

Okay quick fix:

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:

Edit 9/7/16 -- The file now looks like this:

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

And mine works properly like this, but my situation isn't exactly like the OP's.


Original answer -- 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')()
    )

Aha - open /usr/local/bin/ipython in your editor (with privileges), and change the first line to

#!/usr/local/bin/python

save, start iPython, should say it's using the version you want now.

, '', sys.argv[0]) sys.exit(start_ipython())

And mine works properly like this, but my situation isn't exactly like the OP's.


Original answer -- 9/30/13:

Aha - open /usr/local/bin/ipython in your editor (with privileges), and change the first line to

save, start iPython, should say it's using the version you want now.

通知家属抬走 2025-01-15 00:12:18

将 @Matt 的评论作为答案发布,以便其更明显

python -m IPython

将 ipython 加载为模块,并首先在路径上访问任何 python。就我而言,我预装了一个,另一个是我从brew 添加的。这非常有效。

Posting @Matt's comment as an answer just so its more visible

python -m IPython

Loads ipython as a module with whatever python is accessible on the path first. In my case I had one pre-installed and one I added from brew. This just works perfectly.

遮了一弯 2025-01-15 00:12:18

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

创建 virtualenv 时,可以使用 -p 标志指定 python 路径。

对于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

What about using a virtualenv?
I really like it. Maybe it's not the faster way, but I think it's very clear.

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

for python 2.7

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

for python 3.4

$ virtualenv -p /usr/bin/python3.4 venv3.4
$ source venv3.4/bin/activate
(venv3.4)$ pip install ipython
(venv3.4)$ ipython
玩套路吗 2025-01-15 00:12:18

首先,我会确保你使用的是正确的Python。在命令提示符下键入:

which python
python -V

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

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

which python
python -V

The first will tell you the path, the second tells you the Python version you're using.

别低头,皇冠会掉 2025-01-15 00:12:18

我的解决方案很简单,愚蠢但有效。

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

$ python -V
Python 2.7.10

,然后在 .bash_profile 中创建别名

$ vi ~/.bash_profile

添加一行,

alias ipython="python -m IPython"

然后您将在 python 2.7 中得到一个 ipython 。

My solution is simple, stupid but work.

I use python -V to make sure what version is

$ python -V
Python 2.7.10

and then make alias in .bash_profile

$ vi ~/.bash_profile

Add a line

alias ipython="python -m IPython"

then you will get an ipython in python 2.7. ????

(By the way, my ipython is install via homebrew, it default will got an ipython run in python 3.)

$ brew install ipython
阪姬 2025-01-15 00:12:18

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

td;lr 由于 shell“散列”和路径变量而遇到问题。

extremely relevant: http://conda.pydata.org/docs/troubleshooting.html#shell-command-location.

td;lr problems are encountered because of shell 'hashing' and path variables.

随波逐流 2025-01-15 00:12:18

使用 pyenv 的类似方法

pyenv install 3.4.5
pyenv local 3.4.5
pip install ipython
ipython

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

Python 3.4.5

A similar method using pyenv

pyenv install 3.4.5
pyenv local 3.4.5
pip install ipython
ipython

Now it will show correct version of python

Python 3.4.5
冰雪之触 2025-01-15 00:12:18

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

  1. 可执行的 ipython 实际上是一个 Python 脚本的事实。
  2. IPython 包是为您运行 pip intall 的每个解释器单独安装的。

如果您运行的 Python 版本安装了 IPython 软件包,您只需执行

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

此操作即可使用您想要的解释器(而不是 shebang 中列出的解释器)运行 ipython 脚本。

The absolute simplest solution I could think of, which requires no fiddling with environments, installed files, or anything else, relies on the facts that

  1. The executable ipython is actually a Python script.
  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)

This will run the ipython script with the interpreter you want instead of the one listed in the shebang.

他不在意 2025-01-15 00:12:18

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

所以解决问题的方法是让 ipython 使用正确的 python (安装了 scipy 这样的库)

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

如何让 iPython 使用 Python 2 而不是 Python 3

Your problem is basically making ipython use the right python.

so the fix to the problem is to make ipython use the right python (which has the libraries like scipy installed)

I have written a solution here:

How to make iPython use Python 2 instead of Python 3

凉城凉梦凉人心 2025-01-15 00:12:18

我遇到了同样的问题,但以下是在 OSX 12、Sierra 上对我有用的唯一解决方案。

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 将修复此问题并让您只需运行 ipython2

对于我来说,因为我很认真地想在 2.7 中使用 ipython,所以我还运行了以下命令。

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

I came across the same issue but the following was the only solution what worked for me on OSX 12, Sierra.

ipython was always launching for python 3.6 but I needed it for 2.7. I could not find an ipython startup script for 2.7, nor could I find the IPython module to execute with python -m. None of brew instally ipython pip install ipython or pip2 install ipython could get me the 2.7 version. So I got it manually.

brew install ipython@5 installs the 2.7 version from here but won't put it on your $PATH because it knows the name conflicts with another package.
ln -s /usr/local/Cellar/ipython@5/5.5.0_1/bin/ipython /usr/local/bin/ipython2 will fix this and let you just run ipython2 from your shell prompt

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
2025-01-15 00:12:18

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

这个答案基于您使用 anaconda 的假设。

假设您处于 python 3 环境中,并且在 jupyter 笔记本上创建笔记本时,它显示“Python 2”而不是“Python 3”。

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

首先停止 ipython 服务器并使用命令“which python”获取当前环境的 python 可执行文件的位置

我的输出是:

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

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

我的是:

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

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

现在导航到目录 anaconda2/bin (对于 anaconda 3 用户,它应该是 anaconda3/bin )并搜索“ipython”文件。在此编辑第一行,将其指向您想要的当前 python 版本。即“which python”的输出,即:

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

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

保存文件。并运行 jupyter Notebook,现在在创建新笔记本时,“Python 3”选项应该可见。

干杯!

All the answers mentioned here do not help in solving the issue if you are using anaconda or some other virtual environment wrapper.

This answer is based on the assumption that you are using anaconda.

Say you are in a python 3 environment and when creating a notebook on jupyter notebook it shows "Python 2" instead of "Python 3".

This is because "ipython" is essentially a script which is run and in this script it mentions which python version it is using to execute the command. All you need to do is change this line for ipython to use the version of python you want.

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

My output is :

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

Now get the executable location of ipython use the command "which ipython"

mine is :

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

Notice that it is using a different version of python ie. python from a specific environment running a different version of python ie running python from a different environment.

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

Notice that I changed my python environment from py2(running python 2.7) to py3(running python 3.5).

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

Cheers!

忘羡 2025-01-15 00:12:18

对于我来说,在 Windows 10 上,我的 ipython.exe 与我的 python 不匹配。
我知道这一点是因为 ipython 说

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

ModuleNotFoundError: No module named 'pywinauto'

但是,当我使用 > 执行包含程序时,这有效。 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. 我在设置中更改了我的系统路径 - 环境变量
  • C :\Users\joeco\AppData\Roaming\Python\Python38\Scripts
  • 现在 C:\Users\joeco\AppData\Roaming\Python\Python39\Scripts\
  1. MOVED 上面的值 UP 到路径顶部
  2. REBOOTED

RESULTS

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

For me, on windows 10, my ipython.exe did not match my python.
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

Here's the mis-match.

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

Here's my fix.
I changed my reference to Python\...\Scripts, which is where ipython.exe is hiding

  1. I CHANGED my system PATH in settings - environment variables
  • was C:\Users\joeco\AppData\Roaming\Python\Python38\Scripts
  • now C:\Users\joeco\AppData\Roaming\Python\Python39\Scripts\
  1. MOVED the above value UP to the top of the path
  2. REBOOTED

RESULTS

  1. ipython now matches python
  2. ipython from pywinauto import handleprops SUCCEEDS
傾城如夢未必闌珊 2025-01-15 00:12:18

我安装了 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)?

所以我只是在同一位置制作了该文件的两个副本,名称为 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

现在,当我需要启动 python2 时,我只需从终端运行 ipython,并以相同的方式在 python3.8 中获取 ipython,我运行 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]: 

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

, '', sys.argv[0]) sys.exit(start_ipython())

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

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

现在,当我需要启动 python2 时,我只需从终端运行 ipython,并以相同的方式在 python3.8 中获取 ipython,我运行 ipython3.8

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

I had python2.7, python3.6 and python3.8 installed

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

I had ipython file located at this location

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

It was having below content

#!/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)?

So I just made two copies of that file at the same location with name ipython3.6 and ipython3.8 and updated the first line to point to specific python version, /usr/bin/python3.6 and /usr/bin/python3.8 in my case.

So there are now 3 files each pointing to specific python version

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]: 

Note: you may need to install ipython for specific python version using command python3.6 -m pip install IPython or python3.8 -m pip install IPython. I am not sure whether it was installed with python or I installed separately.

, '', sys.argv[0]) sys.exit(start_ipython())

So I just made two copies of that file at the same location with name ipython3.6 and ipython3.8 and updated the first line to point to specific python version, /usr/bin/python3.6 and /usr/bin/python3.8 in my case.

So there are now 3 files each pointing to specific python version

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

Note: you may need to install ipython for specific python version using command python3.6 -m pip install IPython or python3.8 -m pip install IPython. I am not sure whether it was installed with python or I installed separately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文