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

2018-12-31 21:39发布

问题:

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?

回答1:

Changing the default python version system wide would break some applications that depend on python2.

You can alias the commands in most shells, Mac OS X uses bash by default, if you also do put this into your ~/.bash_profile:

alias python=\'python3\'

python command now refers to python3. If you want the original python (that refers to python2), you can escape the alias i.e. doing \\python will launch python2 leaving the alias untouched)

If you launch interpreters more often (I do), better is to:

alias 2=\'python2\'
alias 3=\'python3\'

Tip: Instead of doing:

#!/usr/bin/env python

use:

#!/usr/bin/env python3

system will use python3 for running python executables.



回答2:

You can solve it by symbolic link.

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


回答3:

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?



回答4:

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.



回答5:

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



回答6:

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.



回答7:

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