如何在 MacOS 上将 Python 3.5 设为我的默认版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34528107/
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
How do I make Python 3.5 my default version on MacOS?
提问by user3798654
I have just installed Python 3.5.1 on my Mac (running the latest version of OSX). My system came with Python 2.7 installed. When I type IDLE
at the Terminal prompt my system pulls up the original Python 2.7 rather than the newly installed Python 3.5. How do I get my system to default to Python 3.5.1 when I open the IDLE window from Terminal?
我刚刚在 Mac 上安装了 Python 3.5.1(运行最新版本的 OSX)。我的系统安装了 Python 2.7。当我IDLE
在终端提示符下输入时,我的系统会启动原始的 Python 2.7 而不是新安装的 Python 3.5。当我从终端打开 IDLE 窗口时,如何让我的系统默认为 Python 3.5.1?
采纳答案by Railslide
Since Python 2 and 3 can happily coexist on the same system, you can easily switch between them by specifying in your commands when you want to use Python 3.
由于 Python 2 和 3 可以在同一系统上愉快地共存,因此您可以通过在命令中指定何时要使用 Python 3 来轻松地在它们之间切换。
So for Idle, you need to type idle3
in the terminal in order to use it with Python 3 and idle
for using it with Python 2.
因此,对于 Idle,您需要idle3
在终端中输入才能在 Python 3 和idle
Python 2 中使用它。
Similarly, if you need to run a script or reach a python prompt from the terminal you should type python3
when you want to use Python 3 and python
when you want to use Python 2.
类似地,如果您需要运行脚本或从终端到达 python 提示符,则应键入python3
何时要使用 Python 3 和python
何时要使用 Python 2。
回答by Lorenzo Cioni
You can use the python3
command (instead of using python
), or you can simply uninstall the 2.7 version if you don't use it
您可以使用python3
命令(而不是使用python
),或者如果您不使用它,您可以简单地卸载 2.7 版本
回答by Olivier Van de Velde
If you dont have any python 2 scripts that you use, you can delete python2. But its not a problem to have them both installed. You just have to use another path python3
to launch IDLE.
如果您没有任何使用的 python 2 脚本,则可以删除 python2。但是安装它们都不是问题。您只需要使用另一个路径python3
来启动 IDLE。
I would prefer to let them both installled so if you have any scripts that are in python 2 you can still run them or you have to port them to python3.
我更愿意让它们都安装,所以如果你有任何在 python 2 中的脚本,你仍然可以运行它们,或者你必须将它们移植到 python3。
回答by 0-_-0
By typing python
, you are actually referring to a link.
You will find its location with $ which python
. In my case it was /usr/local/bin/python
. go there $open /usr/local/bin/
and just delete the original python, python-config and idle as they are
identical to the 2.7 files in the same folder.
Then duplicate the 3.5 files and rename them to what you just deleted.
通过键入python
,您实际上是指一个链接。你会用 找到它的位置$ which python
。就我而言,它是/usr/local/bin/python
. 去那里$open /usr/local/bin/
,只需删除原始的 python、python-config 和 idle,因为它们与同一文件夹中的 2.7 文件相同。然后复制 3.5 文件并将它们重命名为您刚刚删除的内容。
This also changes the default link other editors like Sublime_ReplPython use and updates it therefore to the 3.5 Version. This was my major concern with the standard installation.
这也会更改其他编辑器(如 Sublime_ReplPython)使用的默认链接,并将其更新为 3.5 版本。这是我对标准安装的主要关注点。
回答by Naseer-shaik
You can switch to any python version in your project by creating a virtual environment.
您可以通过创建虚拟环境切换到项目中的任何 Python 版本。
- virtualenv -p /usr/bin/python2.x (or python 3.x)
- virtualenv -p /usr/bin/python2.x(或python 3.x)
In case you just want to run a program in a specific version just open shell and enter python2.x or python3.x
如果您只想在特定版本中运行程序,只需打开 shell 并输入 python2.x 或 python3.x
回答by Edison
It's good practice to have your MacOS Python environment set up properly from the beginning making sure that Homebrew installations take precedence over stock MacOS binaries. You want it in usr/local/bin
not MacOS default usr/bin
.
从一开始就正确设置您的 MacOS Python 环境是一种很好的做法,以确保 Homebrew 安装优先于库存的 MacOS 二进制文件。你想要它usr/local/bin
不是 MacOS default usr/bin
。
.bash_profile
.bash_profile
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
Can also create aliases for both.
也可以为两者创建别名。
alias py2='python2.7'
alias py3='python3.6'
Source the file to ensure it takes effect for the current session
获取文件以确保它对当前会话生效
source ~/.bash_profile
Homebrew install and setup etc...
Homebrew 安装和设置等...
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew update
brew upgrade --all
brew cleanup
Python3 install
Python3 安装
brew install python3
Next
下一个
pip3 install virtualenv
Next
下一个
pip3 install virtualenvwrapper
When all is finished python3
, pip3
, virtualenv
, and virtualenvwrapper.sh
will all be in usr/local/bin
.
当所有完成后python3
,pip3
,virtualenv
,和virtualenvwrapper.sh
都将是usr/local/bin
。
Result
结果
Every time I install anything or use commands like mkvirtualenv
Python 3 is used by default.
每次我安装任何东西或使用mkvirtualenv
Python 3 之类的命令时,都会默认使用。
回答by McAllister Bowman
Do right thing, do thing right!
做正确的事,做正确的事!
Open your terminal,
input
python -V
, It likely shows:Python 2.7.10
input
python3 -V
, It likely shows:Python 3.7.2
input
where python
orwhich python
, It likely shows:/usr/bin/python
input
where python3
orwhich python3
, It likely shows:/usr/local/bin/python3
add the following line at the bottom of your PATH environment variable file in ~/.profilefile or ~/.bash_profileunder Bash or ~/.zshrcunder zsh.
打开你的终端,
input
python -V
,它可能显示:Python 2.7.10
input
python3 -V
,它可能显示:Python 3.7.2
输入
where python
orwhich python
,它可能显示:/usr/bin/python
输入
where python3
orwhich python3
,它可能显示:/usr/local/bin/python3
在 Bash 下的~/.profile文件或~/.bash_profile或zsh 下的~/.zshrc中的 PATH 环境变量文件底部添加以下行。
alias python='/usr/local/bin/python3'
OR
alias python=python3
alias python='/usr/local/bin/python3'
或者
alias python=python3
input source ~/.bash_profileunder Bash or source ~/.zshrcunder zsh.
Quit the terminal.
Open your terminal, and input
python -V
, It likely shows:Python 3.7.2
输入源〜/ .bash_profile中下击或源〜/ .zshrc下的zsh。
退出终端。
打开你的终端,输入
python -V
,它可能显示:Python 3.7.2
Note, the ~/.bash_profile under zsh is not that ~/.bash_profile.
注意,zsh下的~/.bash_profile不是~/.bash_profile。
The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.
zsh 下的 PATH 环境变量通过 ~/.zshrc 代替 ~/.profile(或 ~/.bash_file)。
Hope this helped you all!
希望这对大家有帮助!