banner



How To Set Python 3 As Default Mac

How to Install Python 3 on Mac – Brew Install Update Tutorial

MacOS comes with Python pre-installed. Just information technology's Python Version 2.7, which is at present deprecated (abandoned past the Python developer customs).

The unabridged Python community has now moved on to using Python three.x (the current version equally of writing this is 3.9). And Python 4.x volition be out soon, but it volition exist completely backward compatible.

If you endeavor to run Python from your MacOS terminal, yous'll even see this alarm:

freecodecamp_-_freecodecamp_MacBook-Pro_-___-_-zsh_-_84-24-1
WARNING: Python 2.7 is non recommended. This version is included in macOS for compatibility with legacy software. Future versions of macOS will not include Python 2.7. Instead, it is recommended that you transition to using 'python3' from within Terminal.

Until Apple decides to ready Python iii.10, equally the default you lot're going to accept to install it yourself.

A Single Command to Run Python three

For some of you reading this, this control may be enough. Y'all can run Python three using this control (with the 3 at the end).

                python3              

If that's all you lot came for, no worries. Have a fun day and happy coding.

Simply if you want a proper Python version control system to keep rails of various versions – and have fine-grain control over which version yous use – this tutorial volition show you exactly how to accomplish this.

Megaman-810x600
By the mode, if you're wondering why I keep referring to Python iii.x – the x is a stand up-in for sub-versions (or point releases equally developers call them.) This ways whatever version of Python 3.

How to Install Homebrew on Mac

First you lot need to install Homebrew, a powerful bundle manager for Mac.

Open up upwardly your terminal. Yous can do this by using MacOS spotlight (control+infinite) and typing "last".

At present that you're in a command line, yous can install the latest version of Homebrew by running this control:

                /bin/bash -c "$(gyre -fsSL https://raw.githubusercontent.com/Homebrew/install/Caput/install.sh)"              

Your final volition enquire for Super User-level access. You lot will need to type your countersign to run this command. This is the aforementioned password you type when you lot log into your Mac. Type it and hit enter.

freecodecamp_-__bin_bash_-c__-__bin_bash_-_sudo_-_bash_-c____bin_bash_012set_-u_012_012abort___-_012__printf___s_n_______012__exit_1_012-_012_012if___-z___-BASH_VERSION_--_____then_012__abort__Bash_is_required_to_interpret_this_script___012
A screenshot of my heavily customized concluding. Your terminal will probably await different from this.

Homebrew volition ask you to ostend you desire to install the following. Yous have to press enter to continue. (Or press any other fundamental if you become cold feet.)

freecodecamp_-__bin_bash_-c__-__bin_bash_-_bash_-c____bin_bash_012set_-u_012_012abort___-_012__printf___s_n_______012__exit_1_012-_012_012if___-z___-BASH_VERSION_--_____then_012__abort__Bash_is_required_to_interpret_this_script___012fi_012_

How to Install pyenv to Manage Your Python Versions

Now allow'south take a moment to install PyEnv. This library will assistance yous switch between different versions of Python (in case you demand to run Python two.x for some reason, and in anticipation of Python 4.0 coming).

Run this command:

                brew install pyenv              
freecodecamp_-_freecodecamp_MacBook-Pro_-___-_-zsh_-_90-24
PyEnv installing

At present you can install the latest version of Python.

How to Use pyenv to Install Python or Update Your Python Version

Now you just need to run the following control:

                pyenv install 3.nine.2                              

Annotation that you tin substitute 3.ix.ii for whatever the latest version of Python is. For instance, once Python 4.0.0 comes out, you can run this:

                pyenv install 4.0.0              

Troubleshooting pyenv Installation

If you lot run into an error that "C compiler cannot create executables" so the simplest way to solve this is to reinstall Apple's Xcode.

Xcode is a tool created by Apple that includes all the C libraries and other tools that Python uses when it runs on MacOS. Xcode is a whopping 11 gigabytes, but y'all'll want to exist up-to-date. You lot may want to run this while y'all're sleeping.

You lot can get the latest version of Apple's Xcode here. I had to do this after upgrading to MacOS Big Sur, but once I did, all the post-obit commands worked fine. Just re-run the above pyenv install 3.ix.2 and it should now piece of work.

How to Set Your MacOS PATH for pyenv (Bash or ZSH)

Commencement yous need to update your Unix path to pave a manner for PyEnv to be able to collaborate with your arrangement.

This is a long caption of how PATH works in MacOS (and Unix), straight from the pyenv GitHub repo.

When you lot run a command like python or pip, your operating system searches through a list of directories to find an executable file with that proper noun. This list of directories lives in an environment variable called PATH, with each directory in the listing separated by a colon:
                /usr/local/bin:/usr/bin:/bin                              
Directories in PATH are searched from left to correct, so a matching executable in a directory at the beginning of the listing takes precedence over another 1 at the finish. In this example, the /usr/local/bin directory will be searched first, and so /usr/bin, then /bin.

And here is their explanation of what a Shim is. I'thou quoting them at length once again considering I actually tin can't explain this meliorate myself.

pyenv works past inserting a directory of shims at the front of your PATH:
                $(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin                              
Through a procedure called rehashing, pyenv maintains shims in that directory to match every Python command beyond every installed version of Python—python, pip, and then on.
Shims are lightweight executables that merely pass your command along to pyenv.

Here's how to update your .bash_profile in Bash (which is installed in MacOS by default):

                repeat 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile              

Or if you lot've installed ZSH (or OhMyZSH) like I have, you'll desire to edit the .zshrc file instead:

                echo 'consign PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc              

Then you want to add PyEnv Init to your terminal. Run this command if yous're using Bash (again, this is the default with MacOS):

                echo -east 'if command -v pyenv 1>/dev/nada 2>&one; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile                              

Or run this command if you're using ZSH:

                echo -e 'if command -v pyenv 1>/dev/null 2>&one; then\northward  eval "$(pyenv init --path)"\northward  eval "$(pyenv init -)"\nfi' >> ~/.zshrc              

At present reset your concluding by running this command:

                reset              

How to Set a Version of Python to Global Default (Fustigate or ZSH)

You lot tin can set up the latest version of Python to be global, pregnant it will be the default version of Python MacOS uses when you run Python applications.

Run this control:

                pyenv global iii.9.2              

Again, you can replace 3.9.ii with whatsoever the latest version is.

At present you can verify that this worked past checking the global version of Python:

                pyenv versions              

Yous should see this output:

freecodecamp_-_freecodecamp_MacBook-Pro_-_-zsh_-_84-24
The * means that version is now the global default

The Final Step: Shut Your Terminal and Restart it

Once you've restarted your concluding, yous run the python command and y'all'll launch the new version of Python instead of the old i.

freecodecamp_-_python_-_python_-_python_-_119-36
Yay. Python 3.nine.two and no deprecation warnings

Congratulations. Give thanks you for reading this, and happy coding.



Learn to code for free. freeCodeCamp's open source curriculum has helped more than twoscore,000 people get jobs as developers. Become started

Source: https://www.freecodecamp.org/news/python-version-on-mac-update/

0 Response to "How To Set Python 3 As Default Mac"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel