问题:如何在OS X上将Python的默认版本设置为3.x?
我正在运行Mountain Lion,而基本的默认Python版本是2.7。我下载了Python 3.3,并希望将其设置为默认值。
目前:
$ python
version 2.7.5
$ python3.3
version 3.3
如何设置它,以便每次运行$ python
时都能打开3.3?
I’m running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default.
Currently:
$ python
version 2.7.5
$ python3.3
version 3.3
How do I set it so that every time I run $ python
it opens 3.3?
回答 0
在系统范围内更改默认python可执行文件的版本可能会破坏某些依赖python2的应用程序。
但是,您可以在大多数外壳程序中为命令加上别名,因为macOS中的默认外壳程序(10.14及以下版本中的bash; 10.15中的zsh)具有相似的语法。您可以在您的中放置别名python =’python3′ ~/.profile
,然后~/.profile
在您~/.bash_profile
和/或您的源代码中~/.zsh_profile
输入以下内容:
[ -e ~/.profile ] && . ~/.profile
这样,您的别名将可在所有shell中使用。
这样,python
命令现在将被调用python3
。如果您想偶尔调用“原始” python(指python2),则可以使用command python
,这将使别名保持不变,并且适用于所有shell。
如果您更频繁地启动解释器(我愿意),则总是可以创建更多别名来添加,即:
alias 2='python2'
alias 3='python3'
提示:对于脚本,而不是使用shebang之类的方法:
#!/usr/bin/env python
采用:
#!/usr/bin/env python3
这样,系统将使用python3运行python 可执行文件。
Changing the default python executable’s version system-wide could break some applications that depend on python2.
However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put
alias python=’python3′
in your ~/.profile
, and then source ~/.profile
in your ~/.bash_profile
and/or your~/.zsh_profile
with a line like:
[ -e ~/.profile ] && . ~/.profile
This way, your alias will work across shells.
With this, python
command now invokes python3
. If you want to invoke the “original” python (that refers to python2) on occasion, you can use command python
, which will leaving the alias untouched, and works in all shells.
If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:
alias 2='python2'
alias 3='python3'
Tip: For scripts, instead of using a shebang like:
#!/usr/bin/env python
use:
#!/usr/bin/env python3
This way, the system will use python3 for running python executables.
回答 1
您可以通过符号链接来解决。
unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
You can solve it by symbolic link.
unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
回答 2
打开〜/ .bash_profile文件。
vi ~/.bash_profile
然后按如下所示放置别名:
alias python='python3'
现在保存文件,然后运行〜/ .bash_profile文件。
source ~/.bash_profile
恭喜!!!现在,您可以通过键入python使用python3 。
python --version
的Python 3.7.3
Open ~/.bash_profile file.
vi ~/.bash_profile
Then put the alias as follows:
alias python='python3'
Now save the file and then run the ~/.bash_profile file.
source ~/.bash_profile
Congratulation !!! Now, you can use python3 by typing python.
python --version
Python 3.7.3
回答 3
转到终端类型:
alias python=python3.x
这会将默认python设置为python3.x
Go to terminal type:
alias python=python3.x
This will setup default python as python3.x
回答 4
以下为我工作
cd /usr/local/bin
mv python python.old
ln -s python3 python
The following worked for me
cd /usr/local/bin
mv python python.old
ln -s python3 python
回答 5
我在这个游戏上有点晚了,但是我认为我应该发布更新的答案,因为我自己才遇到这个问题。请注意,这仅适用于基于Mac的设置(我没有在Windows或任何版本的Linux上尝试过)。
最简单的方法是通过Brew安装Python 。如果您未安装brew,则需要先执行该操作。安装完成后,在终端上执行以下操作:
brew install python
这将安装Python3。安装后,运行以下命令:
ls -l /usr/local/bin/python*
您将看到brew创建的所有安装到其Python安装的链接。它看起来像这样:
lrwxr-xr-x 1 username admin 36 Oct 1 13:35 /usr/local/bin/python3@ -> ../Cellar/python/3.7.4_1/bin/python3
lrwxr-xr-x 1 username admin 43 Oct 1 13:35 /usr/local/bin/python3-config@ -> ../Cellar/python/3.7.4_1/bin/python3-config
lrwxr-xr-x 1 username admin 38 Oct 1 13:35 /usr/local/bin/python3.7@ -> ../Cellar/python/3.7.4_1/bin/python3.7
lrwxr-xr-x 1 username admin 45 Oct 1 13:35 /usr/local/bin/python3.7-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7-config
lrwxr-xr-x 1 username admin 39 Oct 1 13:35 /usr/local/bin/python3.7m@ -> ../Cellar/python/3.7.4_1/bin/python3.7m
lrwxr-xr-x 1 username admin 46 Oct 1 13:35 /usr/local/bin/python3.7m-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7m-config
此示例的第一行显示了python3
符号链接。要将其设置为默认python
符号链接,请运行以下命令:
ln -s -f /usr/local/bin/python3 /usr/local/bin/python
设置后,您可以执行以下操作:
which python
它应该显示:
/usr/local/bin/python
您将必须重新加载当前的终端shell,才能在该shell中使用新的符号链接,但是,所有新打开的shell会话将(应该)自动使用它。要对此进行测试,请打开一个新的终端外壳并运行以下命令:
python --version
I’m a little late to the game on this one, but I thought I should post an updated answer since I just encountered this issue for myself. Please note that this will only apply to a Mac-based setup (I haven’t tried it with Windows or any flavor of Linux).
The simplest way to get this working is to install Python via Brew. If you don’t have brew installed, you will need to do that first. Once installed, do the following in at the terminal:
brew install python
This will install Python 3. After it’s installed, run this:
ls -l /usr/local/bin/python*
You will see all of the links created by brew to its Python install. It will look something like this:
lrwxr-xr-x 1 username admin 36 Oct 1 13:35 /usr/local/bin/python3@ -> ../Cellar/python/3.7.4_1/bin/python3
lrwxr-xr-x 1 username admin 43 Oct 1 13:35 /usr/local/bin/python3-config@ -> ../Cellar/python/3.7.4_1/bin/python3-config
lrwxr-xr-x 1 username admin 38 Oct 1 13:35 /usr/local/bin/python3.7@ -> ../Cellar/python/3.7.4_1/bin/python3.7
lrwxr-xr-x 1 username admin 45 Oct 1 13:35 /usr/local/bin/python3.7-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7-config
lrwxr-xr-x 1 username admin 39 Oct 1 13:35 /usr/local/bin/python3.7m@ -> ../Cellar/python/3.7.4_1/bin/python3.7m
lrwxr-xr-x 1 username admin 46 Oct 1 13:35 /usr/local/bin/python3.7m-config@ -> ../Cellar/python/3.7.4_1/bin/python3.7m-config
The first row in this example shows the python3
symlink. To set it as the default python
symlink run the following:
ln -s -f /usr/local/bin/python3 /usr/local/bin/python
Once set, you can do:
which python
and it should show:
/usr/local/bin/python
You will have to reload your current terminal shell for it to use the new symlink in that shell, however, all newly opened shell sessions will (should) automatically use it. To test this, open a new terminal shell and run the following:
python --version
回答 6
转到“应用程序”,进入“ Python”文件夹,应该有一个名为“ Update Shell Profile.command”或类似名称的bash脚本。运行该脚本,它应该这样做。
更新:看来您不应该更新它:如何更改默认python版本?
Go to ‘Applications’, enter ‘Python’ folder, there should be a bash script called ‘Update Shell Profile.command’ or similar. Run that script and it should do it.
Update: It looks like you should not update it: how to change default python version?
回答 7
这对我有用。我添加了别名并重新启动了终端:
alias python=/usr/local/bin/python3
This worked for me. I added alias and restarted my terminal:
alias python=/usr/local/bin/python3
回答 8
我相信大多数登陆这里的人都在使用ZSH thorugh iterm或其他工具,这为您带来了答案。
您必须改为添加/修改命令~/.zshrc
。
I believe most of people landed here are using ZSH thorugh iterm or whatever, and that brings you to this answer.
You have to add/modify your commands in ~/.zshrc
instead.
回答 9
我不确定在OS X上是否可用,但是在Linux上我会使用该module
命令。 看这里。
正确设置modulefile,然后将以下内容添加到rc文件中(例如〜/ .bashrc):
module load python3.3
这样一来,登录时即可根据需要切换路径,而不会影响任何系统默认值。
I’m not sure if this is available on OS X, but on linux I would make use of the module
command. See here.
Set up the modulefile correctly, then add something like this to your rc file (e.g. ~/.bashrc):
module load python3.3
This will make it so that your paths get switched around as required when you log in without impacting any system defaults.
回答 10
我认为安装python时会将导出路径语句放入〜/ .bash_profile文件中。因此,如果您不再打算使用Python 2,则可以从那里删除该语句。如上所述的别名也是一种很好的方法。
这是从〜/ .bash_profile中删除引用的方法-vim ./.bash_profile-删除引用(也类似:export PATH =“ / Users / bla / anaconda:$ PATH”)-保存并退出-源./ .bash_profile保存更改
I think when you install python it puts export path statements into your ~/.bash_profile file. So if you do not intend to use Python 2 anymore you can just remove that statement from there. Alias as stated above is also a great way to do it.
Here is how to remove the reference from ~/.bash_profile
– vim ./.bash_profile
– remove the reference (AKA something like: export PATH=”/Users/bla/anaconda:$PATH”)
– save and exit
– source ./.bash_profile to save the changes
回答 11
$ sudo ln -s -f $(which python3) $(which python)
完成。
$ sudo ln -s -f $(which python3) $(which python)
done.
回答 12
回答 13
如果virtualenvwrapper
使用which virtualenvwrapper.sh
,则可以使用进行定位,然后使用vim
或任何其他编辑器将其打开,然后更改以下内容
# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
将行更改VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
为VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"
。
If you are using a virtualenvwrapper
, you can just locate it using which virtualenvwrapper.sh
, then open it using vim
or any other editor then change the following
# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
Change the line VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
to VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"
.
回答 14
对我来说,解决方案是使用PyCharm并将默认的python版本设置为我需要使用的版本。
安装PyCharm并转到文件==>新项目的首选项,然后为项目选择所需的解释器,在这种情况下为python 3.3
For me the solution was using PyCharm and setting the default python version to the the one that i need to work with.
install PyCharm and go to file ==> preferences for new project, then choose the interpreter you want for your projects, in this case python 3.3
回答 15
如果您使用macports,则不需要使用别名或环境变量,只需使用macports已经提供的方法,此问答将对此进行说明:
作法:Macports选择python
TL; DR:
sudo port select --set python python27
If you use macports, you do not need to play with aliases or environment variables, just use the the method macports already offers, explained by this Q&A:
How to: Macports select python
TL;DR:
sudo port select --set python python27
回答 16
如果您使用的是Macports,则有一种更简单的方法:
跑:
port install python37
安装后,设置默认值:
sudo port select --set python python37
sudo port select --set python3 python37
重新启动您的cmd窗口,完成。
If you are using macports, that has a easier way to do:
run:
port install python37
after install, set default:
sudo port select --set python python37
sudo port select --set python3 python37
restart your cmd window, finished.
回答 17
好吧…有点老了。但是仍然值得一个好的答案。
优点之一是您不想触摸Mac上的默认Python。
通过Homebrew或其他方式安装所需的任何Python版本,然后在virtualenv中使用它。Virtualenv通常被认为是胡扯,但还是比在系统范围内更改python版本(macOS可能会保护自己免受此类操作)或用户范围,bash范围……好得多。只需忘记默认的Python。使用venv这样的游乐场是您的操作系统最感谢的事情。
例如,这种情况是,许多现代Linux发行版都摆脱了现成安装的Python2的安装,仅在系统中保留了Python3。但是每次您尝试使用python2作为依赖项安装旧版本时…希望您理解我的意思。一个好的开发者不在乎。好的开发人员可以使用他们想要的python版本创建干净的游乐场。
Well… It’s kinda old. But still deserves a good answer.
And the good one is You Don’t Wanna Touch The Default Python On Mac.
Install any Python version you need via Homebrew or whatever and use it in virtualenv. Virtualenv is often considered to be something crap-like, but it’s still way, wayyyy better than changing python version system-wide (macOS is likely to protect itself from such actions) or user-wide, bash-wide… whatever. Just forget about the default Python. Using playgrounds like venv is what your OS will be most, very most grateful for.
The case is, for example, many modern Linux distributions get rid of Python2 installed out-of-the-box, leaving only Python3 in the system. But everytime you try to install something old with python2 as a dependency… hope you understand what I mean. A good developer doesn’t care. Good developers create clean playgrounds with python version they desire.
回答 18
Mac用户只需要在终端上运行以下代码
brew switch python 3.x.x
3.xx应该是新的python版本。
这将更新所有系统链接。
Mac users just need to run the following code on terminal
brew switch python 3.x.x
3.x.x should be the new python version.
This will update all the system links.
回答 19
建议将python别名为python3会导致设置python版本的虚拟环境出现问题(例如:pyenv)。使用pyenv,您可以像下面这样全局设置版本:
pyenv global 3.8.2
然后在任何特定项目中,您都可以创建一个.python-version文件,其中包含python版本:
pyenv local 2.7.1
我认为这是在系统上管理多个版本的python的最佳方法。
Suggestions to alias python to python3 will cause problems with virtual environments that set the version of python (eg: pyenv). With pyenv, you can set the version globally like so:
pyenv global 3.8.2
and then in any specific project, you can create a .python-version file which has the python version inside of it:
pyenv local 2.7.1
This is the best way to manage multiple versions of python on a system in my opinion.