问题:ipython读取错误的python版本
我在使用Python,iPython和库时遇到了麻烦。以下几点显示了问题的链。我在Mac Lion上运行Python 2.7。
- iPython不会读取scipy和matplotlib的库,但会读取numpy。
- 为了解决这个问题,我尝试安装Python的源代码版本,这给我带来了更多问题,因为现在我有两个不同的版本:2.7.1和2.7.2
- 我注意到运行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.
- iPython doesn’t read the libraries of scipy, matplotlib, but it does read numpy.
- 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
- 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.
回答 0
好的快速修复:
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)?$', '', sys.argv[0])
sys.exit(start_ipython())
我的工作正常,但是我的情况与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,应该说它正在使用您现在想要的版本。
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)?$', '', 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:
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.
回答 1
发表@Matt的评论作为答案,这样它才更明显
python -m IPython
首先使用路径上可访问的任何python将ipython加载为模块。就我而言,我预装了一个,然后从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.
回答 2
怎么样使用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
回答 3
首先,我将确保您使用的是正确的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.
回答 4
我的解决方案简单,愚蠢但可行。
我python -V
用来确定是什么版本
$ python -V
Python 2.7.10
然后在.bash_profile中创建别名
$ vi ~/.bash_profile
添加一行
alias ipython="python -m IPython"
那么您将ipython
在python 2.7中获得一个。🙂
(顺便说一句,我ipython
是通过安装的homebrew
,默认情况下将在python 3中运行一个ipython。)
$ brew install 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
回答 5
回答 6
使用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
回答 7
我能想到的绝对最简单的解决方案,即无需摆弄环境,安装的文件或其他任何东西,它依赖于以下事实:
- 该可执行文件
ipython
实际上是一个Python脚本。
- IPython包是与您使用的每个解释器分开安装的
pip intall
。
如果您运行的Python版本安装了IPython软件包,则可以
/path/to/desired/python $(which ipython)
这将ipython
使用所需的解释器而不是shebang中列出的解释器来运行脚本。
The absolute simplest solution I could think of, which requires no fiddling with environments, installed files, or anything else, relies on the facts that
- The executable
ipython
is actually a Python script.
- 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.
回答 8
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
回答 9
我遇到了同样的问题,但以下是在OSX 12 Sierra上对我有效的唯一解决方案。
ipython始终为python 3.6启动,但我需要2.7。我找不到2.7的ipython启动脚本,也找不到与一起执行的IPython模块python -m
。没有一个brew instally ipython
pip install ipython
或pip2 install ipython
可以让我获得2.7版本的功能。所以我手动得到了。
brew install ipython@5
从此处安装2.7版本,但不会安装在您的2.7版本上,$PATH
因为它知道名称与另一个软件包冲突。
ln -s /usr/local/Cellar/ipython@5/5.5.0_1/bin/ipython /usr/local/bin/ipython2
将解决此问题,让您仅ipython2
在shell提示符下运行
对我来说,因为我认真考虑将python用于2.7,所以我还运行了以下命令。
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
回答 10
如果您使用的是anaconda或其他虚拟环境包装器,则此处提到的所有答案都无助于解决问题。
该答案基于您正在使用anaconda的假设。
假设您处于python 3环境中,并且在jupyter笔记本上创建笔记本时,它将显示“ Python 2”而不是“ Python 3”。
这是因为“ ipython”本质上是一个正在运行的脚本,并且在此脚本中提到了用于执行命令的python版本。您需要做的就是更改此行,以便ipython使用所需的python版本。
首先停止ipython服务器,并使用命令“哪个python”获取当前环境的python可执行文件的位置。
我的输出是:
/home/sourabh/anaconda2/envs/py3/bin/python
现在使用命令“哪个ipython”获取ipython的可执行位置。
我的是 :
/home/sourabh/anaconda2/envs/py2/bin/python
请注意,它使用的是不同版本的python即。来自运行不同版本python的特定环境的python,即运行来自不同环境的python。
现在,导航到目录anaconda2 / bin(对于anaconda 3用户,应为anaconda3 / bin)并搜索“ ipython”文件。在此编辑中,将第一行指向您想要的当前python版本。即“哪个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!