May 02, 2020 tldr; install brew; brew install python. Homebrew is a great installer for Mac and its main job is to make it easier to install packages. Step 1: Download Homebrew here 'brew' hosts its package information here as “formulae“. Step 2: Use brew to install the latest version of Python $ brew install python -verbose. Installation happens with the brew command, which gives us access to thousands of command-line utilities, but not more complex applications. For us nerds, the utilities that fit into 'the stuff you need' category include the handy tree command that shows directory structures and pyenv, which I use to manage multiple versions of Python.
Question or problem about Python programming:
How can I install a previous version of Python 3 in macOS using brew?
With the command brew install python I got the latest version of Python 3 (currently v3.7.0), but I want the last version of Python 3.6 (currently 3.6.5).
In today’s blog post I demonstrated how to install OpenCV 3 with Python 2.7 and Python 3 bindings on your macOS system via Homebrew. As you can see, utilizing Homebrew is a great method to avoid the tedious process of manually configuring your CMake command to compile OpenCV via source (my full list of OpenCV install tutorials can be found on this page). Now could create a python 3.6.51 virtual environment to use it in a specific project, and then let brew update your python installation. To achieve that result with the module venv use the -copies option to make sure copies of the binaries (python, pip, etc.) were copied to the virtual environment folder.
I have read about another package pyenv that can assist in handle different python installation, but this solution is not suitable for me.
How to solve the problem:
Homebrew provides Python 3.6 for macOS: $ brew install python3 To create an isolated Python installation use virtualenv: $ virtualenv venv/eskapade -python = python3.
To make a clean install of Python 3.6.5 use:
If you prefer to recover a previously installed version, then:
There are two formulas for installing Python with Homebrew:
[email protected] and
The first is for Python 2 and the second for Python 3.
Note: You can find outdated answers on the web where it is mentioned
python3 as the formula name for installing Python version 3. Now it’s just
By default, with these formulas you can install the latest version of the corresponding major version of Python. So, you cannot directly install a minor version like 3.6.
brew, you can install a package using the address of the formula, for example in a git repository.
Or specifically for Python 3
The address you must specify is the address to the last commit of the formula (python.rb) for the desired version.
You can find the commint identifier by looking at the history for homebrew-core/Formula/python.rb
Python > 3.6.5
In the link above you will not find a formula for a version of Python above 3.6.5.
After the maintainers of that (official) repository released Python 3.7, they only submit updates to the recipe of Python 3.7.
As explained above, with homebrew you have only Python 2 ([email protected]) and Python 3 (python), there is no explicit formula for Python 3.6.
Although those minor updates are mostly irrelevant in most cases and for most users, I will search if someone has done an explicit formula for 3.6.
As an update, when doing
You may encounter
To bypass it, add the
--ignore-dependencies argument to brew install.
What I did was first I installed python 3.7
then I installed python 3.6.5 using above link
After that I ran
brew link --overwrite python. Now I have all pythons in the system to create the virtual environments.
To create Python 3.7 virtual environment.
To create Python 3.6 virtual environment
I have tried everything but could not make it work. Finally I have used
pyenv and it worked directly like a charm.
homebrew installed, juste do:
to manage virtualenvs:
See pyenv and pyenv-virtualenv for more info.
I have found using the pyenv-installer easier than homebrew to install pyenv and pyenv-virtualenv direclty:
To manage python version, either globally:
or locally in a given directory:
I tried all the answers above to install Python 3.4.4. The installation of python worked, but PIP would not be installed and nothing I could do to make it work. I was using Mac OSX Mojave, which cause some issues with zlib, openssl.
What not to do:
- Try to avoid using Homebrew for previous version given by the formula Python or Python3.
- Do not try to compile Python
- Download the macOS 64-bit installer or macOS 64-bit/32-bit installer: https://www.python.org/downloads/release/python-365/
- In previous step, it will download Python 3.6.5, if for example, you want to download Python 3.4.4, replace in the url above python-365 by python-344
- Download click on the file you downloaded a GUI installer will open
- If you downloaded python-365, after installation, to launch this version of python, you will type in your terminal python365, same thing for pip, it will be pip365
p.s: You don’t have to uninstall your other version of Python on your system.
I found a much much much better solution that work on MacOSX, Windows, Linux, etc.
- It doesn’t matter if you have already python installed or not.
- Download Anaconda
- Once installed, in terminal type:
- In terminal,create virtual environment with any python version, for example, I picked 3.4.4:
conda create -n [NameOfYour VirtualEnvironment] python=3.4.4
- Then, in terminal, you can check all the virtual environment you ahave created with the command:
conda info --envs
- Then, in terminal, activate the virtual environment of your choice with:
conda activate [The name of your virtual environment that was shown with the command at step 5]
Hope this helps!
pyenv lets you manage multiple versions of Python on your computer.
This blog post focuses on how pyenv uses the shim design pattern to provide a wonderful user experience (it doesn’t focus on installing and using the software like other posts).
Once pyenv is installed you can easily switch between one project that uses Python 3.6, another project that uses Python 3.7, and a system-wide default Python, without any additional thought.
Managing Python versions and packages can be a huge pain. It’s important to understand how pyenv works to debug issues. Complexities are layered on top of different Python versions… good luck understanding virtual environments if you don’t understand Python versioning.
pyenv is a fork of rbenv, a project to manage Ruby versions. The “shim design philosophy” used by pyenv and rbenv has proven to be a winner for maintaining multiple versions of a programming language on a given machine. Learning the shim design philosophy will make you a better programmer and teach you a powerful design pattern you can use with your own programs.
You can install pyenv with Homebrew using the
brew install pyenv command.
Homebrew runs this formula. We’ll cover how Homebrew works in another post.
which pyenv to see that there is a pyenv executable in the
open /usr/local/bin to view the
pyenv executable in the directory.
Homebrew Python 3.6 Free
pyenv executable is run whenever we run commands like
pyenv versions or
pyenv install --list. Simply run
pyenv without any arguments to see a listing of all the pyenv commands that can be run.
Let’s dig into where pyenv installs Python code in the filesystem.
pyenv versions to see the Python versions you currently have installed on your machine.
pyenv install --list to see the Python versions that can be installed.
One of the Python versions that can be installed is
3.6.10 – let’s install it with
pyenv install 3.6.10. Here’s the console output:
Python 3.6.10 was downloaded to the
pyenv versions to see all the Python versions available on your machine.
My machine has the system Python version, 3.6.10, and 3.7.5.
Python 3.7.5 is the “current selected” version of Python that pyenv will use (as indicated by the * next to 3.7.5 when
pyenv versions is run).
If you run
3.7.5 is the selected Python version, then a Python 3.7.5 shell will be started. Understanding how pyenv knows to launch a 3.7.5 shell when the
python command is run is the central focus of this post.
/Users/matthewpowers/.pyenv/version file only contains a single line of data.
Let’s view the current “global” python version with
The global Python version is set in the
Let’s change the global version to
pyenv global 3.6.10.
We can run
pyenv versions and see that pyenv is using 3.6.10 by default now.
Let’s inspect the contents of the
/Users/matthewpowers/.pyenv/version file to decipher what the
pyenv global 3.6.10 command did under the hood.
pyenv global 3.6.10 just clobbered the
/Users/matthewpowers/.pyenv/version file with a new Python version number.
Let’s make a bad decision and clobber
> to see if we can change the global Python version ourselves (don’t do this – we’re just hacking to learn how this all works).
The pyenv global version is set in the
/Users/matthewpowers/.pyenv/version file. This approach simple and intuitive. Let’s see how we can modify Python versions for different projects.
Let’s create a
~/Documents/project1/ folder and cd into the directory.
We can run
pyenv version to see this project is using Python 3.7.5 (it’s using the global Python version by default):
Let’s change the Python version to 3.6.10 for this project.
pyenv added a
.python-version file to the project1 directory.
Let’s inspect the contents of
/Users/matthewpowers/.pyenv/version file that sets the global Python version is unchanged.
pyenv is somehow performing an analysis like “yes, I can see you have a global Python version set, but the
python version command is being run from the
project1 directory, and that has a
.python-version file that takes precedence over the global Python version”.
Let’s create a
~/Documents/project2/ folder and set the local Python version to “system”.
Let’s verify that project2 is using the system Python version.
Homebrew Python 3.6 Full
Let’s run some commands to recap and demonstrate that the home directory is using a Python version of 3.7.5, project1 is using 3.6.10, and project2 is using the system Python.
Let’s keep digging and see how pyenv is switching these Python versions magically.
PATH is an environment variable that specifies an ordered list of folders where executables are saved.
echo $PATH on your machine to see the path on your machine.
PATH is an ordered list of directories delimited by a colon. It’s easier to visualize a bulleted list:
When you enter a command in Terminal, your computer will look for an executable in
/usr/local/bin first, then
/bin/usr/sbin and finally
When you run
echo hi, your Terminal will start by looking for an executable named
echo in the
/usr/local/bin directory. There isn’t a
echo executable in
open /usr/local/bin/ on your machine and visually inspect to verify).
echo isn’t in
/usr/bin either. An
echo executable is stored in
/bin. The Terminal will use whatever executable it finds first.
You can also find where executables are located with the
pyenv changes PATH
pyenv adds this code to the
~/.bash_profile which changes the
PATH on your machine:
echo $PATH to see the
PATH is different now:
/Users/matthewpowers/.pyenv/shims directory has been added before all the other directories in the
All Terminal commands will go through
/Users/matthewpowers/.pyenv/shims first now. This allows pyenv to “intercept” any relevant Python commands.
$ pythoncommand will go to
$ pipcommand will go to
/Users/matthewpowers/.pyenv/shims doesn’t contain an executable named
echo, so the
echo hi there command will still use the
echo executable that’s in
Rehashing design pattern
Once you have pyenv installed and run a simple command like
pip install pandas, what exactly happens? How does pyenv execute this code?
The pyenv README has a great high level description on shims and the rehashing pattern. From the README:
Shims are lightweight executables that simply pass your command along to pyenv. So with pyenv installed, when you run, say,
pip, your operating system will do the following:
- Search your PATH for an executable file named pip
- Find the pyenv shim named pip at the beginning of your PATH
- Run the shim named pip, which in turn passes the command along to pyenv
Let’s describe these steps in more detail:
- When you run
pip install pandas, your Terminal will search your
pipexecutable. It’ll find that executable in the
/Users/matthewpowers/.pyenv/shimsdirectory that is at the front of your path. pyenv is intercepting this command and handling it before it can be picked up by another executable lower in the
pythoncommand will use the executable in
/usr/bin, the dreaded system Python version, it it’s not intercepted.
/Users/matthewpowers/.pyenv/shims/pipis an executable file that’s referred to as a “shim”. A shim intercepts a call and redirects the operation elsewhere.
- The shim redirects the command to pyenv. Let’s take a look at the code with
We can see that
exec '/usr/local/Cellar/pyenv/1.2.18/libexec/pyenv' is “passing the command along to pyenv”.
Intercepting the python command
python command will be intercepted by
/Users/matthewpowers/.pyenv/shims/python, as you can see by running
The code in
/Users/matthewpowers/.pyenv/shims/python is exactly the same as the code in
This is what
cat /Users/matthewpowers/.pyenv/shims/python returns:
The shim passes the command to pyenv without doing any processing.
Let’s look at the pyenv executable that’s being passed commands from the shims.
cat /usr/local/Cellar/pyenv/1.2.18/libexec/pyenv to inspect the contents of the
pyenv executable. Prepare yourself for 150 lines of Bash code.
Here is where this script is defined in the pyenv repo.
pip install for different Python versions
Suppose you’re using Python 3.7.5 and run
pip install pandas.
The Pandas code will be stored in
If you switch to Python 3.6.10 with
pyenv shell 3.6.10, you’ll need to reinstall pandas with another
pip install pandas. The Python 3.6.10 pandas will be stored in
pyenv doesn’t share library versions across different Python versions.
See pyenv-virtualenv for more details about managing virtual environments with pyenv.
pyenv is not bootstrapped by Python
You might wonder why the pyenv codebase is almost entirely Shell and Roff code.
pyenv intentionally avoids Python. If pyenv was written in Python, then the system would need Python installed to run pyenv commands. pyenv can install Python on machines that don’t have any version of Python installed (most machines come with an old Python version pre-installed).
Homebrew Python 3.6 Tutorial
You don’t want a program that installs Python to depend on Python.
The system version of Python that comes pre-installed in machines causes a huge headache for programmers that don’t understand the PATH, executable, etc. The shell scripting patterns you’ve learned in this post will save you from a lot of Python development hell.
Homebrew Install Python 3.6
We learned a lot about pyenv and have a good understanding of how it works.
The shims are injected at the beginning of your PATH so pyenv can route commands to the right Python version. pyenv is using versions specified in text files to see what version of Python should be used to run the commands.
pyenv offers an elegant user interface for managing different Python versions for end users. The codebase is amazingly clean.
Homebrew Python 3.6 Free
Programs like these make you sit back with awe – you’re starstruck that other programmers can architect such beauty.
Brew Install Python 3.6.5_1
Study their design patterns and try to copy them! Copy what you like.
This page describes how Python is handled in Homebrew for users. See Python for Formula Authors for advice on writing formulae to install packages written in Python.
Homebrew should work with any CPython and defaults to the macOS system Python.
Homebrew provides formulae to brew Python 3.x.
Homebrew provided a
[email protected] formula until the end of 2019, at which point it was removed due to the Python 2 deprecation.
Important: If you choose to use a Python which isn’t either of these two (system Python or brewed Python), the Homebrew team cannot support any breakage that may occur.
Homebrew provides a formula for Python 3.x (
Important: Python may be upgraded to a newer version at any time. Consider using a versionmanager such as
pyenv if you require stability of minor or patch versions for virtual environments.
The executables are organised as follows:
python3points to Homebrew’s Python 3.x (if installed)
pip3points to Homebrew’s Python 3.x’s pip (if installed)
Unversioned symlinks for
pip etc. are installed here:
Setuptools, Pip, etc.
The Python formulae install pip (as
pip3) and Setuptools.
Setuptools can be updated via pip3, without having to re-brew Python:
Similarly, pip3 can be used to upgrade itself via:
site-packages and the
site-packages is a directory that contains Python modules (especially bindings installed by other formulae). Homebrew creates it here:
So, for Python 3.y.z, you’ll find it at
Python 3.y also searches for modules in:
Homebrew Install Wii
site-packages directory is first created if (1) any Homebrew formula with Python bindings are installed, or (2) upon
brew install python.
The reasoning for this location is to preserve your modules between (minor) upgrades or re-installations of Python. Additionally, Homebrew has a strict policy never to write stuff outside of the
brew --prefix, so we don’t spam your system.
Homebrew-provided Python bindings
Some formulae provide Python bindings.
Warning! Python may crash (see Common Issues) if you
import <module> from a brewed Python if you ran
brew install <formula_with_python_bindings> against the system Python. If you decide to switch to the brewed Python, then reinstall all formulae with Python bindings (e.g.
Policy for non-brewed Python bindings
Homebrew Install Python 3.8
These should be installed via
pip install <package>. To discover, you can use
pip search or https://pypi.python.org/pypi.
Note: macOS’s system Python does not provide
pip. Follow the pip documentation to install it for your system Python if you would like it.
Brewed Python modules
For brewed Python, modules installed with
python3 setup.py install will be installed to the
$(brew --prefix)/lib/pythonX.Y/site-packages directory (explained above). Executable Python scripts will be in
Use Homebrew To Install Python
The system Python may not know which compiler flags to set in order to build bindings for software installed in Homebrew so you may need to run:
Homebrew Install Python 3.7
WARNING: When you
brew install formulae that provide Python bindings, you should not be in an active virtual environment.
Activate the virtualenv after you’ve brewed, or brew in a fresh terminal window.Homebrew will still install Python modules into Homebrew’s
site-packages and not into the virtual environment’s site-package.
Virtualenv has a
--system-site-packages switch to allow “global” (i.e. Homebrew’s)
site-packages to be accessible from within the virtualenv.
Homebrew Install Python 3.8
Why is Homebrew’s Python being installed as a dependency?
Homebrew Install Python Version
Formulae that declare an unconditional dependency on the
'python' formula are bottled against Homebrew’s Python 3.x and require it to be installed.