如何在 OS X 10.6.8 的终端中设置默认 Python 版本?

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

How to set default Python version in terminal on OS X 10.6.8?

pythonmacos

提问by CodeKingPlusPlus

I just downloaded python 2.7 on my mac which runs OS X 10.6.8. The previous version of python installed was 2.6.

我刚刚在运行 OS X 10.6.8 的 Mac 上下载了 python 2.7。之前安装的python版本是2.6。

When I type pythonin the terminal it opens python version 2.6. I want it to open python 2.7

当我python在终端中输入时,它会打开 python 2.6 版。我想让它打开 python 2.7

How do I make Python 2.7 open by default?

如何让 Python 2.7 默认打开?

回答by waitingkuo

Add followings to your ~/.bash_profile

将以下内容添加到您的 ~/.bash_profile

# Setting PATH for Python 2.7
PATH="/path/to/your/python2.7/bin:${PATH}"
export PATH

Save the file and reopen the terminal.

保存文件并重新打开终端。

回答by Daniil Ryzhkov

The easier solution is to install it via MacPorts:

更简单的解决方案是通过 MacPorts 安装它:

sudo port install python_select
port search python # Search for version you are looking for
sudo port install python27
sudo port select --set python python27

回答by Ned Deily

The python.org installers for Python 2.x on OS X by default modify shell profiles (for the standard shells like bashand csh) to add its framework bindirectory to the front of your shell path. Assuming you did not deselect the option during installation, there should now be the following in your .bash_profilefile.

默认情况下,OS X 上 Python 2.x 的 python.org 安装程序会修改 shell 配置文件(对于标准 shell,如bashcsh),以将其框架bin目录添加到 shell 路径的前面。假设您在安装过程中没有取消选择该选项,现在您的.bash_profile文件中应该有以下内容。

# Setting PATH for Python 2.7
# The orginal version is saved in .profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

But this profile is only executed by default when you launch a new terminal window; it won't apply to existing terminal sessions. So make sure you open a new one and then try again. If you are using a different shell, you may need to modify that shell's startup to do the equivalent.

但是此配置文件仅在您启动新终端窗口时默认执行;它不适用于现有的终端会话。所以一定要打开一个新的,然后再试一次。如果您使用不同的 shell,则可能需要修改该 shell 的启动以执行等效操作。

The python.org installers for Python 3.x on OS X do not select the shell script modification option by default. You can enable it at installation or you can later run the Update Shell Profile.commandfile in the corresponding Python x.xfolder in the Applicationsfolder. Or you can just manually edit the right profile.

默认情况下,OS X 上 Python 3.x 的 python.org 安装程序不选择 shell 脚本修改选项。您可以在安装时启用它,也可以稍后运行Update Shell Profile.command文件Python x.x夹中相应文件夹中的Applications文件。或者您可以手动编辑正确的配置文件。