How make Python 3 default on a Mac [duplicate] - python

Currently running Mac OS X Lion 10.7.5 , and it has python2.7 as default. In the terminal, i type 'python' and it automatically pulls up python2.7. I don't want that.
from terminal I have to instead type 'python3.2' if i want to use python3.2.
How do i change that?

The safest way is to set an alias in ~/.bashrc:
alias python=python3
That way you avoid breaking things for scripts relaying on python being python2.

You could edit the default python path and point it to python3.2
Open up ~/.bash_profile in an editor and edit it so it looks like
PATH="/Library/Frameworks/Python.framework/Versions/3.2/bin:${PATH}"
export PATH

If you have python 2 and 3 on brew. Following worked for me.
brew unlink python#2
brew link python#3 (if not yet linked)

Related

How to change default Python 2.7.10 to Python 3.6.2 on macOS? [duplicate]

This question already has answers here:
How to change default Python version?
(19 answers)
Closed 1 year ago.
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?
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.
You can solve it by symbolic link.
unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
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
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
Go to terminal type:
alias python=python3.x
This will setup default python as python3.x
This worked for me. I added alias and restarted my terminal:
alias python=/usr/local/bin/python3
The following worked for me
cd /usr/local/bin
mv python python.old
ln -s python3 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?
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.
$ sudo ln -s -f $(which python3) $(which python)
done.
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
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.
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
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.
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!
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.
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
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
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)".
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.
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.

Homebrew macOS - Python installation

I've installed python via homebrew. It is located in:
/usr/local/Cellar/python/2.7.13_1
which should be right.
Now I am trying to use this python installation, but "which python" only shows the macOS python installation at "/usr/bin/python". So i am checking the $PATH and I see that everything should be ok.
"echo $PATH" results in this: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I restarted the terminal window and this occurs every time. I also did the
"brew doctor" and no warnings appeared.
What I am using:
Standard macOS Terminal-App
Has anybody a clue how this problem could be solved?
Update $PATH in your .bashrc file.
Example add the following line in your .bashrc
export PATH=/usr/local/Cellar/python/2.7.13_1/bin:$PATH
Either add the Python Homebrew prefix to your $PATH, like #Zico suggests in his answer, or link the Python executable into /usr/local/bin (which might already be in your path)
You'd do that with
$ brew link python
Good luck :)
Brew create an python2 alias to
/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7
I just created a copy of python2 alias and rename it to python. That solved the problem

Switch between python 2.7 and python 3.5 on Mac OS X

I generally use Python 2.7 but recently installed Python 3.5 using Miniconda on Mac OS X. Different libraries have been installed for these two versions of python. Now, the entering either of the keywords 'python' or 'python3' in terminal invokes python 3.5, and 'python2' returns '-bash: python2: command not found'. How can I now invoke them specifically using aliases 'python2' and 'python3' respectively?
I am currently using OS X El Capitan.
IMHO, the best way to use two different Python versions on macOS is via homebrew. After installing homebrew on macOS, run the commands below on your terminal.
brew install python#2
brew install python
Now you can run Python 2.7 by invoking python2 or Python 3 by invoking python3. In addition to this, you can use virtualenv or pyenv to manage different versions of python environments.
I have never personally used miniconda but from the documentation, it looks like it is similar to using pip and virtualenv in combination.
OSX's Python binary (version 2) is located at /usr/bin/python
if you use which python it will tell you where the python command is being resolved to. Typically, what happens is third parties redefine things in /usr/local/bin (which takes precedence, by default over /usr/bin). To fix, you can either run /usr/bin/python directly to use 2.x or find the errant redefinition (probably in /usr/local/bin or somewhere else in your PATH)
I already had python3 installed(via miniconda3) and needed to install python2 alongside in that case brew install python won't install python2, so you would need
brew install python#2 .
Now alias python2 refers to python2.x from /usr/bin/python
and alias python3 refers to python3.x from /Users/ishandutta2007/miniconda3/bin/python
and alias python refers to python3 by default.
Now to use python as alias for python2, I added the following to .bashrc file
alias python='/usr/bin/python'.
To go back to python3 as default just remove this line when required.
Similar to John Wilkey's answer I would run python2 by finding which python, something like using /usr/bin/python and then creating an alias in .bash_profile:
alias python2="/usr/bin/python"
I can now run python3 by calling python and python2 by calling python2.
Here is how to set the Python version back to 2.7 if you have installed Anaconda3 (Python 3.6) on MacOS High Sierra 10.13.5.
Edit the .bash_profile file in your home directory.
vi $HOME/.bash_profile
Comment out the line with anaconda3 addition to your PATH like this:
# export PATH="/Users/YOURUSERNAME/anaconda3/bin:$PATH"
Close the shell, then open it again
You should now see 2.7 when you run python
Then, if you want 3.6 you can simply uncomment out the anaconda3 line in your .bash_profile.
Trying to unlink python will end in tears in Mac OS X.
You will get something like this
unlink: /usr/bin/python: Operation not permitted
I just follow up the answer from #John Wilkey.
My alias python used to represent python2.7 (located in /usr/bin).
However the default python_path is now preceded by /usr/local/bin for python3; hence when typing python, I didn't get either the python version.
I tried make a link in /usr/local/bin for python2:
ln -s /usr/bin/python /usr/local/bin/
It works when calling python for python2.
If you want to use Apple’s system install of Python 2.7, be aware that it doesn’t quite follow the naming standards laid out in PEP 394.
In particular, it includes the optional symlinks with suffix 2.7 that you’re told not to rely on, and does not include the recommended symlinks with suffix 2 that you’re told you should rely on.
If you want to fix this, while sticking with Apple’s Python, you can create your own symlinks:
$ cd <somewhere writable and in your PATH>
$ ln -s /usr/bin/python python2
Or aliases in your bash config:
alias python2 python2.7
And you can do likewise for Apple’s 2to3, easy_install, etc. if you need them.
You shouldn’t try to put these symlinks into /usr/bin, and definitely don’t try to rename what’s already there, or to change the distutils setup to something more PEP-compliant. Those files are all part of the OS, and can be used by other parts of the OS, and your changes can be overwritten on even a minor update from 10.13.5 to 10.13.6 or something, so leave them alone and work around them as described above.
Alternatively, you could:
Just use python2.7 instead of python2 on the command line and in your shbangs and so on.
Use virtual environments or conda environments. The global python, python3, python2, etc. don’t matter when you’re always using the activated environment’s local python.
Stop using Apple’s 2.7 and instead install a whole other 2.7 alongside it, as most of the other answers suggest. (I don’t know why so many of them are also suggesting that you install a second 3.6. That’s just going to add even more confusion, for no benefit.)

Why is Python not updated on Mac after I used homebrew to install Python 3?

I would like to update Python 2.7 to Python 3.4. Currently, I am using Mac OS X 10.9.5.
I have used Homebrew to install Python 3.4. However, when I used python -V to check the version of my Python in the terminal, it still appears to be on the 2.7 version.
Anybody have any idea as for why this is?
Also, I have tried to follow the directions of this article to update to Python 3 (see the second half of the article). Yet my system cannot find this file: /System/Library/Frameworks/Python.framework/Versions/3.4
First install python3 using homebrew.
brew install python3
Second you may want to use python3 as default change the ~/.bash_aliases to point to python3. This can be done by adding the following command in the ~/.bash_aliases.
alias python='python3'
now all the python command gets referred to python3 by default.
EDIT 1:
Try running the following command.
/usr/bin/env python3
This will make python3 as default environment for python executables.
EDIT 2:
[Like suggested by robert]
try making a symlink from python to python3. this makes python3 as default environment in all shells.

setting/changing PYTHONPATH in OS Mavericks

Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which python in Terminal shows the /usr/bin/python directory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2.
"Explain like I'm five".
If you want to install packages for python 3.4 use: pip3 install django
When installing for python 2.7 just use: pip install django
To use python 3.4 type python3 in your shell.
To see where all installations of python are use: which -a python
Depending on how you installed the new versions of python you will see output like:
/usr/local/bin/python
/usr/bin/python
If you wanted to use the python in /usr/local/bin/python you can edit your .bashrc file and add export path="/usr/local/bin:$path".
Save, then type source .bashrc in your shell and when you type which python it will show something like /usr/local/bin/python
Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems.
You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.
To update PYTHONPATH you can run from the terminal:
export PYTHONPATH=$PYTHONPATH:/desired/path/to/add
Then to check the updated PYTHONPATH you can run:
echo $PYTHONPATH
I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.

Categories