Download Python 3.6 for Mac Doesn't Work Updated
Download Python 3.6 for Mac Doesn't Work
I've been dipping my toe dorsum into Python evolution as I get ready to head to PyCon US. (If yous're headed there besides and want to share your Python story, permit me know!) When I installed a module to tinker around with, I got a reminder that I needed to install Python three presently.
$ pip install todoist-python
DEPRECATION: Python 2.seven volition reach the end of its life on Jan 1st, 2020. Please upgrade your Python as Python ii.seven won't be maintained after that engagement. A future version of pip will drop support for Python 2.7.
So, I did what any of us would practise and googled around looking for a guide to update my development surroundings, which runs on Mac (the macOS operating system, formerly known every bit OS X). To my surprise, I found only a handful of StackOverflow posts, and they pointed me to partial solutions. Here's the total story of how to fix your environment without breaking anything built into the macOS operating system.
one. Install pyenv
Moshe Zadka cautions that doing this incorrect could result in an unreliable idea of which Python is running that depends also closely on shells loading aliases. I knew Moshe was familiar with Python, but what I didn't know is that he is an author of many Python tutorials as well as an upcoming volume on Python development on macOS. He helped forty colleagues develop Python safely and consistently on macOS systems following one core principle:
"The bones premise of all Python development is to never employ the system Python. You do not want the Mac Os Ten 'default Python' to be 'python3.' You want to never care about default Python."
How do we stop caring nigh the default? Moshe recommends using pyenv to manage Python environments (for a deeper dive on configuring pyenv, see this commodity). This tool manages multiple versions of Python and is described as "simple, unobtrusive, and follows the Unix tradition of single-purpose tools that do one affair well."
While other installation options are available, the easiest way to get started is with Homebrew:
$ brew install pyenv
?/usr/local/Cellar/pyenv/ane.2.x: 634 files, two.4MB
2. Install Python
Now allow's install the latest Python version (3.vii.3 equally of this writing):
$ pyenv install 3.seven.3
python-build: use openssl 1.0 from homebrew
python-build: utilize readline from homebrew
Downloading Python-3.7.3.tar.xz...
-> https://www.python.org/ftp/python/3.7.3/Python-three.7.three.tar.xz
Installing Python-3.7.three...
## farther output not included ##
3. Ready your global default
Now that Python 3 is installed through pyenv, we want to set it equally our global default version for pyenv environments:
$ pyenv global iii.7.3
# and verify it worked
$ pyenv version
3.seven.3 ( set by /Users/mbbroberg/.pyenv/version)
The power of pyenv comes from its control over our crush's path. In gild for it to piece of work correctly, we need to add the following to our configuration file (.zshrc for me, possibly .bash_profile for yous):
$ echo -due east 'if command -v pyenv 1>/dev/null 2>&1; and so\due north eval "$(pyenv init -)"\nfi' >> ~/.zshrc After that command, our dotfile (.zshrc for zsh or .bash_profile for Bash) should include these lines:
if command -v pyenv i >/dev/nix 2 >& one; and then
eval "$(pyenv init -)"
fi
Now we know for certain that we're using Python three.7.3 and pip volition update alongside it without whatever manual aliasing between versions. Using Moshe's recommendation to use a version manager (pyenv) enables us to easily have hereafter upgrades without getting confused nearly which Python we are running at a given time.
Success
As you go comfortable with this workflow, you tin can use pyenv to manage multiple versions of Python. It'due south as well essential, for dependency direction, to utilize virtual environments. I mention how to use the built in venv library in the article, and Moshe recommendsvirtualenvwrapper for managing virtual environments.
Understanding Python runtimes
Now that you have your Python versions fixed, information technology'due south safe to explore why this problem confuses so many people.
The version of Python that ships with macOS is well out of appointment from what Python recommends using for evolution. Pondering Python runtimes can exist comically challenging at times, every bit noted past XKCD.
Many users have dozens of Python interpreters on their estimator already, but accept no idea how to manage them effectively. Besides often, people but download the latest Python release, move information technology to their path, and telephone call it a solar day (or employ brew install python3, which would do something similar). This can cause breakages downward the line in frustrating ways that can exist difficult to troubleshoot.
What Not to exercise
My first thought on how to make Python three the default Python on my system was to move the old version and add the new ane:
# what I thought would piece of work
# get-go, I'll find my python binary
$ which python
/usr/bin/python
# next, I'll motion information technology to an unused proper name
$ sudo mv /usr/bin/python /usr/bin/python2
# lastly, I'll move the new binary to the previous path
$ sudo mv $PATHTOBINARY /python3 /usr/bin/python
The pattern followed what /usr/bin/ usually does between major releases of Python, but I quickly learned it was the incorrect motility:
$ sudomv /usr/bin/python /usr/bin/python2
mv: rename /usr/bin/python to /usr/bin/python2: Operation non permitted
Thankfully, macOS protected me from breaking something I don't fully sympathise. Further enquiry proves this is exactly what we shouldn't do.
Another thing not to try
At present that we know what not to practise, let'due south await at what we could do. At that place are a couple options when we think about common installation patterns for applications on macOS.
Use Python three equally the macOS default
Python's website has a macOS Python 3 installer nosotros can download and employ. If we use the parcel installation, a python3 fill volition exist available in /usr/local/bin/.
Aliasing is a must since the Python binary stored in /usr/bin/ tin can't be changed. What'due south nice nigh an alias is that information technology's specific to our command-line beat. Since I use zsh by default, I put the following into the .zshrc file:
$ echo "alias python=/usr/local/bin/python3.vii" >> ~/.zshrc If you lot are using the default Fustigate vanquish, you lot tin can append this aforementioned text to your .bashrc:
$ echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc This strategy works, but it isn't ideal for making futurity updates to Python. It means nosotros have to remember to cheque the website and download the new files since Python doesn't include a command-line mode to update.
Have Homebrew manage Python iii
The Homebrew project provides a free and open source package director for macOS that many people rely on. Information technology gives Apple users a ability similar to apt-get or yum. If you are a Homebrew user, you lot may already accept Python installed. To quickly bank check, run:
$ brew list | grep python
python
If Python shows up under the command, information technology's installed. What version is it? Let's check:
$ mash info python
python: stable three.7.3 (bottled), HEAD
Interpreted, interactive, object-oriented programming linguistic communication
https://www.python.org/
/usr/local/Cellar/python/iii.7.2_1 ( 8,437 files, 118MB) *
## further output not included ##
Okay, great! The Homebrew maintainers have updated the default Python bottle to point to the latest release. Since the Homebrew maintainers are more than dependable at updating the release than most of united states of america, we can use Homebrew's version of Python 3 with the following control:
$ brew update && mash upgrade python Now we want to point our alias (from above) to the re-create of Python that Homebrew manages:
# If you added the previous alias, use a text editor to update the line to the post-obit
alias python=/usr/local/bin/python3
To make sure the path above points to where Homebrew installed Python in our environment, we tin run brew info python and look for the path information.
This method, of using Homebrew to manage our Python surroundings, is a expert starting place, and information technology fabricated sense to me at the time.
What if we still demand Python 2?
It makes sense for anyone new to Python to begin with Python 3. Only those of us who nonetheless need Python 2—for instance, to contribute to a Python project that'southward only available in Python 2—can continue to utilize the default macOS Python binary available in /usr/bin/python:
$ /usr/bin/python
>>> impress ( "This runtime still works!" )
This runtime notwithstanding works!
Homebrew is so wonderful, it even offers a different formula for Python ii:
# If you lot demand Homebrew'southward Python 2.vii run
$ brew install python@two
At any time, we can remove the aliases from our shell's configuration file to become back to using the default copy of Python on the system.
Don't forget to update pip to pip3!
The pip command is the default package manager specifically for Python packages. Although we inverse our default Python command to be version three, nosotros have to alias our pip command separately if it'due south on the previous version. Beginning, we need to bank check what version nosotros're on:
# Note that this is a upper-case letter V (not lowercase)
$ pip -5
pip 19.0.3 from /Library/Python/ two.7 /site-packages/pip-19.0.iii-py2.vii.egg/pip (python 2.7 )
To ensure we're installing packages compatible with our new version of Python, we'll use another alias to signal to the uniform version of pip. Since we're using Homebrew as our package director in this situation, we know it installed pip3 when we installed Python 3. The default path should be the same as Python 3, merely we can confirm this by asking the beat out to find it:
$ which pip3
/usr/local/bin/pip3
Now that we know the location, we will add together it to our crush configuration file, as we did before:
$ repeat "allonym pip=/usr/local/bin/pip3" >> ~/.zshrc
# or for Bash
$ echo "alias pip=/usr/local/bin/pip3" >> ~/.bashrc
Terminal, we tin confirm that running pip points to pip3 by opening a new shell or by resetting our electric current shell and seeing what we point to:
# This command reloads the current crush without exiting the session
# Alternatively, exit the vanquish and outset a new one
$ exec $0
# Now we tin look to see where pip points us
$ which pip
pip: aliased to /usr/local/bin/pip3
We tin can avoid using Homebrew to update pip, but that requires a much longer tutorial from the Python documentation.
Exercise it right from the offset
If you are simply getting started with Python development on a macOS, do the necessary configurations to make sure you're using the correct version of Python from the start. Installing Python three, with or without Homebrew, and using alias will allow you starting time coding, just it'south not a skillful strategy for the long run. Using pyenv every bit a simple version management solution to get you off to a good start.
This commodity was originally published in May 2019 and has been updated past the editor.
Download Python 3.6 for Mac Doesn't Work
Posted by: brackmanpragnotherse1982.blogspot.com
