在 Mac OS X 上在 python 2.7 和 python 3.5 之间切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34680228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch between python 2.7 and python 3.5 on Mac OS X
提问by Viswanath
I generally use Python 2.7 but recently installed Python 3.5 using Miniconda on Mac OS X. Different libraries have been installed for these two versions of python. Now, the entering either of the keywords 'python' or 'python3' in terminal invokes python 3.5, and 'python2' returns '-bash: python2: command not found'. How can I now invoke them specifically using aliases 'python2' and 'python3' respectively?
我通常使用 Python 2.7,但最近在 Mac OS X 上使用 Miniconda 安装了 Python 3.5。已经为这两个版本的 Python 安装了不同的库。现在,在终端中输入关键字 'python' 或 'python3' 调用 python 3.5,并且 'python2' 返回 '-bash: python2: command not found'。我现在如何分别使用别名“python2”和“python3”来专门调用它们?
I am currently using OS X El Capitan.
我目前正在使用 OS X El Capitan。
回答by John Wilkey
OSX's Python binary (version 2) is located at /usr/bin/python
OSX 的 Python 二进制文件(版本 2)位于 /usr/bin/python
if you use which python
it will tell you where the python
command is being resolved to. Typically, what happens is third parties redefine things in /usr/local/bin
(which takes precedence, by default over /usr/bin
). To fix, you can either run /usr/bin/python
directly to use 2.x or find the errant redefinition (probably in /usr/local/bin
or somewhere else in your PATH
)
如果你使用which python
它会告诉你python
命令被解析到哪里。通常情况下,发生的事情是第三方重新定义事物/usr/local/bin
(优先于 ,默认情况下/usr/bin
)。要修复,您可以运行/usr/bin/python
直接使用2.x或找到错误的重新定义(可能在/usr/local/bin
或在您的其他地方PATH
)
回答by forevergenin
IMHO, the best way to use two different Python versions on macOS is via homebrew
. After installing homebrewon macOS, run the commands below on your terminal.
恕我直言,在 macOS 上使用两个不同 Python 版本的最佳方法是通过homebrew
. 在macOS 上安装自制软件后,在终端上运行以下命令。
brew install python@2
brew install python
Now you can run Python 2.7 by invoking python2
or Python 3 by invoking python3
. In addition to this, you can use virtualenv
or pyenvto manage different versions of python
environments.
现在,您可以通过调用运行 Python 2.7python2
或通过调用python3
. 除此之外,您还可以使用virtualenv
或pyenv来管理不同版本的python
环境。
I have never personally used miniconda
but from the documentation,it looks like it is similar to using pip
and virtualenv
in combination.
回答by Chinh Sutran
I just follow up the answer from @John Wilkey.
我只是跟进@John Wilkey 的回答。
My alias python
used to represent python2.7 (located in /usr/bin
).
However the default python_path is now preceded by /usr/local/bin
for python3
; hence when typing python
, I didn't get either the python version.
我的别名python
用于表示 python2.7(位于/usr/bin
)。然而,默认的 python_path 现在前面是/usr/local/bin
for python3
; 因此在输入时python
,我没有得到 python 版本。
I tried make a link in /usr/local/bin
for python2
:
我试图做一个链接/usr/local/bin
为python2
:
ln -s /usr/bin/python /usr/local/bin/
ln -s /usr/bin/python /usr/local/bin/
It works when calling python
for python2
.
打电话时,它的工作原理python
进行python2
。
回答by ishandutta2007
I already had python3 installed(via miniconda3) and needed to install python2 alongside in that case brew install python
won't install python2, so you would need
brew install python@2
.
我已经安装了 python3(通过 miniconda3)并且需要安装 python2,在这种情况下brew install python
不会安装 python2,所以你需要
brew install python@2
.
Now alias python2
refers to python2.x from /usr/bin/python
现在别名python2
是指 python2.x 来自/usr/bin/python
and alias python3
refers to python3.x from /Users/ishandutta2007/miniconda3/bin/python
和别名python3
是指 python3.x 来自/Users/ishandutta2007/miniconda3/bin/python
and alias python
refers to python3 by default.
别名python
默认是python3。
Now to use python
as alias for python2, I added the following to .bashrc
file
现在python
用作 python2 的别名,我在.bashrc
文件中添加了以下内容
alias python='/usr/bin/python'
.
alias python='/usr/bin/python'
.
To go back to python3 as default just remove this line when required.
要默认返回 python3,只需在需要时删除此行。
回答by Jace
How to set the python version back to 2.7 if you have installed Anaconda3 (Python 3.6) on MacOS High Sierra 10.13.5
如果您在 MacOS High Sierra 10.13.5 上安装了 Anaconda3 (Python 3.6),如何将 python 版本设置回 2.7
Edit the .bash_profile file in your home directory.
编辑主目录中的 .bash_profile 文件。
vi $HOME/.bash_profile
vi $HOME/.bash_profile
hash out the line # export PATH="/Users/YOURUSERNAME/anaconda3/bin:$PATH"
散列 # export PATH="/Users/YOURUSERNAME/anaconda3/bin:$PATH"
Close the shell open again you should see 2.7 when you run python.
再次关闭 shell 打开你应该在运行 python 时看到 2.7。
Then if you want 3.6 you can simply uncomment your anaconda3 line in your bash profile.
然后,如果你想要 3.6,你可以简单地在你的 bash 配置文件中取消注释你的 anaconda3 行。
Trying to unlink python will end in tears in Mac OSX.
在 Mac OSX 中,尝试取消与 python 的链接将以流泪告终。
You will something like this
你会像这样
unlink: /usr/bin/python: Operation not permitted
取消链接:/usr/bin/python:不允许操作
Hope that helps someone out !! :) :)
希望能帮到人!!:) :)
回答by abarnert
If you want to use Apple's system install of Python 2.7, be aware that it doesn't quite follow the naming standards laid out in PEP 394.
如果您想使用 Apple 的 Python 2.7 系统安装,请注意它并不完全遵循PEP 394 中规定的命名标准。
In particular, it includes the optional symlinks with suffix 2.7
that you're told not to rely on, and does not include the recommended symlinks with suffix 2
that you're told you shouldrely on.
特别是,它包括2.7
您被告知不要依赖的带有后缀的可选符号链接,并且不包括2
您被告知应该依赖的带后缀的推荐符号链接。
If you want to fix this, while sticking with Apple's Python, you can create your own symlinks:
如果你想解决这个问题,在坚持使用 Apple 的 Python 的同时,你可以创建自己的符号链接:
$ cd <somewhere writable and in your PATH>
$ ln -s /usr/bin/python python2
Or aliases in your bash config:
或 bash 配置中的别名:
alias python2 python2.7
And you can do likewise for Apple's 2to3, easy_install, etc. if you need them.
如果需要,您也可以对 Apple 的 2to3、easy_install 等进行同样的操作。
You shouldn't try to put these symlinks into /usr/bin
, and definitelydon't try to rename what's already there, or to change the distutils setup to something more PEP-compliant. Those files are all part of the OS, and can be used by other parts of the OS, and your changes can be overwritten on even a minor update from 10.13.5 to 10.13.6 or something, so leave them alone and work around them as described above.
您不应该尝试将这些符号链接放入/usr/bin
,并且绝对不要尝试重命名已经存在的内容,或者将 distutils 设置更改为更符合 PEP 的内容。这些文件都是操作系统的一部分,可以被操作系统的其他部分使用,即使是从 10.13.5 到 10.13.6 或其他版本的小更新,您的更改也可能被覆盖,所以不要管它们并解决它们如上所述。
Alternatively, you could:
或者,您可以:
- Just use
python2.7
instead ofpython2
on the command line and in your shbangs and so on. - Use virtual environments or conda environments. The global
python
,python3
,python2
, etc. don't matter when you're always using the activated environment's localpython
. - Stop using Apple's 2.7 and instead install a whole other 2.7 alongside it, as most of the other answers suggest. (I don't know why so many of them are also suggesting that you install a second 3.6. That's just going to add even moreconfusion, for no benefit.)
- 只需在命令行和 shbangs 中使用
python2.7
而不是使用python2
。 - 使用虚拟环境或 conda 环境。当您始终使用激活环境的 local 时,global
python
、python3
、python2
等并不重要python
。 - 正如大多数其他答案所建议的那样,停止使用 Apple 的 2.7,而是在它旁边安装一个完整的其他 2.7。(我不知道为什么这么多人还建议您安装第二个 3.6。这只会增加更多的混乱,没有任何好处。)
回答by E.Hazledine
Similar to John Wilkey's answer I would run python2 by finding which python
, something like using /usr/bin/python
and then creating an aliasin .bash_profile
:
类似约翰·威尔基的答案,我会通过寻找运行python2 which python
,像使用/usr/bin/python
,然后创建一个别名在.bash_profile
:
alias python2="/usr/bin/python"
alias python2="/usr/bin/python"
I can now run python3 by calling python
and python2 by calling python2
.
我现在可以通过调用来运行 python3,python
通过调用python2
.