如何在 OS X 上将 Python 的默认版本设置为 3.x?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18425379/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:43:29  来源:igfitidea点击:

How to set Python's default version to 3.x on OS X?

pythonpython-3.xmacosinstall

提问by Marcus

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.

我正在运行 Mountain Lion,基本的默认 Python 版本是 2.7。我下载了 Python 3.3 并想将其设置为默认值。

Currently:

目前:

$ python
    version 2.7.5
$ python3.3
    version 3.3

How do I set it so that every time I run $ pythonit opens 3.3?

如何设置它以便每次运行$ python时打开 3.3?

采纳答案by Santosh Kumar

Changing the default python executable's version system-wide could break some applications that depend on python2.

在系统范围内更改默认的 python 可执行文件版本可能会破坏一些依赖于 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 ~/.profilein your ~/.bash_profileand/or your~/.zsh_profilewith a line like:

但是,您可以在大多数 shell 中为命令设置别名,因为 macOS 中的默认 shell(10.14 及更低版本中的 bash;10.15 中的 zsh)共享类似的语法。您可以将 alias python='python3' 放入您的~/.profile,然后~/.profile在您的~/.bash_profile和/或您的源中~/.zsh_profile使用如下一行:

[ -e ~/.profile ] && . ~/.profile

This way, your alias will work across shells.

这样,您的别名将可以跨 shell 工作。

With this, pythoncommand 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.

有了这个,python命令现在调用python3. 如果您想偶尔调用“原始”python(指的是python2),您可以使用command python,这将保持别名不变,并且适用于所有 shell。

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:

提示:对于脚本,不要使用像这样的shebang:

#!/usr/bin/env python

use:

用:

#!/usr/bin/env python3

This way, the system will use python3 for running python executables.

这样,系统将使用 python3 来运行 python可执行文件

回答by CT Zhu

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.

转到“应用程序”,进入“Python”文件夹,应该有一个名为“Update Shell Profile.command”或类似的 bash 脚本。运行该脚本,它应该这样做。

Update: It looks like you should not update it: how to change default python version?

更新:看起来你不应该更新它:如何更改默认的python版本?

回答by Vorticity

I'm not sure if this is available on OS X, but on linux I would make use of the modulecommand. See here.

我不确定这在 OS X 上是否可用,但在 linux 上我会使用该module命令。 见这里

Set up the modulefile correctly, then add something like this to your rc file (e.g. ~/.bashrc):

正确设置模块文件,然后将类似的内容添加到您的 rc 文件中(例如 ~/.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.

这将使您的路径在您登录时根据需要切换,而不会影响任何系统默认值。

回答by doubleOK

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.

我认为当您安装 python 时,它会将导出路径语句放入您的 ~/.bash_profile 文件中。因此,如果您不打算再使用 Python 2,则可以从那里删除该语句。上面提到的别名也是一个很好的方法。

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

以下是如何从 ~/.bash_profile 中删除引用 - vim ./.bash_profile - 删除引用(AKA 类似:export PATH="/Users/bla/anaconda:$PATH") - 保存并退出 - source ./ .bash_profile 保存更改

回答by Shin Kim

You can solve it by symbolic link.

您可以通过符号链接解决它。

unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python

回答by Mr. Crowley

I believe most of people landed here are using ZSH thorugh iterm or whatever, and that brings you to this answer.

我相信来到这里的大多数人都通过 iterm 或其他方式使用 ZSH,这将带给您这个答案

You have to add/modify your commands in ~/.zshrcinstead.

您必须添加/修改您的命令~/.zshrc

回答by Esir Kings

If you are using a virtualenvwrapper, you can just locate it using which virtualenvwrapper.sh, then open it using vimor any other editor then change the following

如果您使用的是virtualenvwrapper,则可以使用 找到它which virtualenvwrapper.sh,然后使用vim或任何其他编辑器打开它,然后更改以下内容

# 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)".

将该行更改VIRTUALENVWRAPPER_PYTHON="$(command \which python)"VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"

回答by Mayank Jain

The following worked for me

以下对我有用

cd /usr/local/bin
mv python python.old
ln -s python3 python

回答by Aditya

Go to terminal type:

转到终端类型:

alias python=python3.x

This will setup default python as python3.x

这会将默认 python 设置为 python3.x

回答by Chama

For me the solution was using PyCharm and setting the default python version to the the one that i need to work with.

对我来说,解决方案是使用 PyCharm 并将默认 python 版本设置为我需要使用的版本。

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

安装 PyCharm 并转到新项目的文件 ==> 首选项,然后为项目选择所需的解释器,在本例中为 python 3.3