How to make Python 3.5 my default version on MacOS?

I just installed Python 3.5.1 on my Mac (works with the latest version of OSX). My system comes with Python 2.7 installed. When I type IDLE at the terminal prompt, my system pulls up the original Python 2.7, not just the installed Python 3.5. How do I configure my default system on Python 3.5.1 when I open an IDLE window from a terminal?

+7
source share
8 answers

Since Python 2 and 3 can happily coexist on the same system, you can easily switch between them by specifying in your commands when you want to use Python 3.

So, for Idle, you need to type idle3 in the terminal to use it with Python 3 and idle to use it with Python 2.

Similarly, if you need to run a script or receive a python prompt from the terminal, you must type python3 if you want to use Python 3 and python if you want to use Python 2.

+13
source

It's good that your MacOS Python environment is set up right from the start, making sure Homebrew installations take precedence over MacOS binaries. You want it in usr/local/bin not on macOS by default usr/bin .

.bash_profile

 # Ensure user-installed binaries take precedence export PATH=/usr/local/bin:$PATH # Load .bashrc if it exists test -f ~/.bashrc && source ~/.bashrc 

Can also create aliases for both.

 alias py2='python2.7' alias py3='python3.6' 

The source of the file to ensure that it takes effect for the current session

 source ~/.bash_profile 

Install and configure Homebrew, etc.

 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew doctor brew update brew upgrade --all brew cleanup 

Python3 installation

 brew install python3 

Further

 pip3 install virtualenv 

Further

 pip3 install virtualenvwrapper 

When everything is completed python3 , pip3 , virtualenv and virtualenvwrapper.sh will all be in usr/local/bin .

Result

Every time I install something or use commands like mkvirtualenv , Python 3 is used by default.

+4
source

You can use the python3 command (instead of using python ) or just uninstall version 2.7 if you are not using it.

+3
source

If you do not have any python 2 scripts, you can remove python2. But this is not a problem when they are both installed. You just need to use a different python3 path to run IDLE.

I would prefer that both of them be installed so if you have any scripts that are in python 2, you can still run them anyway or you need to port them to python3.

+1
source

You can switch to any version of python in your project by creating a virtual environment.

  • virtualenv -p / usr / bin / python2.x (or python 3.x)

If you just want to run the program in a specific version, just open the shell and enter python2.x or python3.x

+1
source

By typing python , you are actually linking to a link. You will find its location with $ which python . In my case, it was /usr/local/bin/python . go there $open /usr/local/bin/ and just delete the original python, python-config and idle as they are identical to 2.7 files in the same folder. Then duplicate the 3.5 files and rename them to what you just deleted.

This also changes the default link for other editors such as Sublime_ReplPython, and therefore updates it to version 3.5. This was my main problem with a standard installation.

0
source

Go to terminal type:

 alias python=python3.x 

This should set Python by default as python3.x

0
source

Do it right, do it right!

  1. Open your terminal,

  2. input python -V most likely shows: Python 2.7.10

  3. python3 -V input most likely shows: Python 3.7.2

  4. input, where python or which python most likely shows: /usr/bin/python

  5. the input, where python3 or which python3 most likely shows: /usr/local/bin/python3

  6. add the following line at the bottom of the PATH environment variable file in the ~ / .profile file or to ~ / .bash_profile in Bash or to ~ / .zshrc in zsh.

alias python='/usr/local/bin/python3' OR alias python=python3

  1. ~ / .bash_profile input source for Bash or ~ / .zshrc source file for zsh.

  2. Exit the terminal.

  3. Open your terminal and python -V probably it shows: Python 3.7.2

Note that ~ / .bash_profile in zsh is not like ~ / .bash_profile.

The PATH environment variable in zsh instead of ~ / .profile (or ~ / .bash_file) via ~ / .zshrc.

Hope this helped you all!

0
source

Source: https://habr.com/ru/post/1239480/


All Articles