ChatGPT解决这个技术问题 Extra ChatGPT

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

This question already has answers here: How to change default Python version? (19 answers) Closed last year.

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?

You can manage the version of python with pyenv.

r
rustyMagnet

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.


Should this not be put in ~/.bash_profile instead of ~/.bash_aliases?
Putting alias python=python3 and then running python in my terminal on osx el capitan didn't work for me. Tried saving it both ~/.bash_aliases and ~/.bash_profile.
@Haymaker87 run source ~/.bash_profile after edit ~/.bash_profile file.
You can do the same for pip: alias pip='pip3.6'
@surfer190 that will cause problem when running with sudo, isn't it?
A
Almog Cohen

You can solve it by symbolic link.

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

This is the correct answer (aliases are nice but only accessible by bash, which limits where you can call from). However, I would use unlink instead of rm to remove symlinks (if you accidentally add a trailing slash on rm you might have some bad results). Alternatively, you could do ln -s -f ... which should overwrite the current symlink.
Does it have any consequences for scripts that expect python to be python2.7?
macOS: unlink: /usr/bin/python: Operation not permitted
@MarksCode try to just run the second original line (i.e. ln -s... with /local/).
For Mac OS users, you should use the brew commands to make the symlinks automatically. For example: brew unlink python, brew switch python 3.x.x_x (or 2.x.x), brew python link python (or python@2). Note, brew installs python 3 as "python3".
A
Ananta Chandra Das

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


If you found like I did that the same issue applies for pip (i.e. the pip --version is still 2.7.x) then you'll need to do the same steps for pip3. vi ~/.bash_profile, alias pip='pip3', source ~/.bash_profile, pip --version
Do I really need to source ~/.bash_profile everytime I re-opened my terminal? It reverts to python2 once I close and open the terminal again.
It worked for me,tkx
@RickyAguilar Me either. My current conda base is 3.8, I want to change the default pythton to 3.10. I have changed the ~/.bash_profile, by commenting out the existing one and adding export PATH="/Applications/anaconda3/envs/py3.10/bin:$PATH" it works after source. However, once I quite the terminal and reopen, it turns out the base is still py3.8. and i need to source again. Which is unresonable. Since I can conda activate py3.10. Have u find another way to tackle this?
if you already installed visual studio code it will be easy to use. code ~/.bash_profile instead of using. vi ~/.bash_profile.
s
sknight

I encountered this issue as well, so I thought I should post an updated answer. 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

You will have to reload your current terminal shell to use the new symlink in that shell. Run this command to reload your shell:

exec $SHELL -l

You're all set now. Now, you can do:

which python

and it should show:

/usr/local/bin/python

All newly opened shell sessions will (should) automatically use the new symlink. To test this, open a new terminal shell and run the following:

python --version

Works great (Reference)
"You will have to reload your current terminal shell for it to use the new symlink in that shell." Thanks, that was the piece I was missing from all the answers so far.
this also worked for me. A few things I would like to add - 1. I am using Mac M1, so I had to run arch -arm64 brew install python & 2. pip wasn't working for me, so I had to run sudo ln -s -f /usr/local/bin/pip3 /usr/local/bin/pip and then pip worked.
@sohammondal Thanks you. I appreciate your input.
A
Aditya

Go to terminal type:

alias python=python3.x

This will setup default python as python3.x


This worked for me but using "alias python=python3"
This will temporarily set python version for that terminal instance.
t
toadead

This worked for me. I added alias and restarted my terminal:

alias python=/usr/local/bin/python3

M
Mayank Jain

The following worked for me

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

u
user3071284

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?


M
Mr. Crowley

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.


o
oori
$ sudo ln -s -f $(which python3) $(which python)

done.


ln: /usr/bin/python: Operation not permitted
S
Sachitha Dilshan

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.

UPDATE

For Newer version of MAC use

brew link python 3.X.X

Not sure why you got a down vote. This worked for me! Thanks for posing this. I upvoted you to cancel that down vote ;)
On Catalina (10.15.17), I get: Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.
this does not work for me
Error: Unknown command: switch
For me it looked like this: brew link python@3.9. Thanks for hints!
j
jacob

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.


d
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.

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


It does not happen
A
ArunDhwaj IIITH

On MacOS

Step-1: Upgrade python to latest version by: $ brew upgrade python

Step-2: Go to home: $ cd

Step-3: open .bash_profile

$ vi .bash_profile

Setting PATH for Python 3.8

PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}" export PATH

Step-4: Save the file. And compile it by:

$ . .bash_profile

Step-5: Check the python version:

$ python -V

Step-6: Thats all.


V
Vorticity

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.


C
ChamCham

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


H
HoldOffHunger

If you use macports, you do not need to play with aliases or environment variables, just use the method macports already offers, explained by this Q&A:

How to: Macports select python

TL;DR:

sudo port select --set python python27

V
Vikas Pandey

This is the simplest way from my exp. (if you have brew installed on your mac).

Try this from your terminal:

brew install python3

and then run the below on your terminal :

ls -l /usr/local/bin/python*

Tip:

** (note down the python version 3.8 or 3.9 thats displayed on the terminal. This will be required in the next step). for e.g. in my case it was:

lrwxr-xr-x 1 user admin 24 May 7 14:33 /usr/local/bin/python -> /usr/local/bin/python3.9

Now run the below command on your terminal:

ln -s -f /usr/local/bin/python3.9 /usr/local/bin/python

(where 3.9 is the version displayed on your terminal with the previous command)

Its DONE !

To test your default version of python:

close the current terminal or start a new terminal and run the below command :

python --version

Happy Coding!


E
Esir Kings

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


I
Istiaque Hossain

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.


T
Tor_Gash

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.