How to Install Multiple Specific Versions of Python on MacOSX

Learn how to install multiple specific versions of python on MacOSX.

Picture of Nsikak Imoh, author of Macsika Blog

Sometimes, you might need to use a Python package that requires a specific version of Python. For instance, you want to use a Python package that requires a specific version of Python.

There are a bunch of ways to do this.

How to Install Multiple Specific Versions of Python Using MacPorts

The first is using Macports as the package manager.

There are separate ports for several python versions 2.x and 3.x currently 2.4, 2.5, 2.6, 2.7 and 3.1, 3.2, 3.3 3.4 and 3.5.

For example, to install 3.4, do sudo port install python34

There is also a python_select port. Running it allows you to choose the Python version to run by /opt/bin/python.

E.g., to select MacPorts python 2.7 port select --set python python27

Note: each package manager needs to run as the only package manager. Otherwise, it will be confused with different versions of libraries and executables on its paths.

With homebrew, the short answer seems to be brew switch the equivalent to port_select.

How to Install Multiple Specific Versions of Python Using Pyenv

  • Install pyenv using homebrew

    brew install pyenv
    
    Install pyenv.
  • Install the specific Python version, run:

    pyenv install <version>
    
    Install specific Python.
  • Select a python version:

    pyenv local 3.5.0
    
    Select a python version.

Note that you must update PATH to override the default Python version:

PATH="~/.pyenv/versions/3.5.0/bin:${PATH}"
Update Path.

To list available versions:

pyenv versions
Current state of the.

You may have seen suggestions to use package managers like brew_python, pyenv, anaconda, or miniconda.

However, what if you want to install multiple Python versions with a symbolic link on MacOSX without a python package manager?

How to Install Multiple Specific Versions of Python Without a Package Manager

You can install this easily using homebrew on a MacOSX terminal.

  • Install the required version:

     % brew install python@3.9
     % brew list | grep python
     % brew ls python@3.9
     % ls -l /usr/local/Cellar/python@3.9/3.9.10/bin/python3.9
    
    Install the required version.
  • Add a soft link to /usr/local/bin/

     % ln -s /usr/local/Cellar/python@3.9/3.9.10/bin/python3.9 /usr/local/bin/python3.9
     % python3.9 -V
    
    Add a soft link.
  • Create a Python virtual environment

     % python3.9 -m venv venv
    
    Create a Python virtual environment.
  • Activate the virtual environment

     % source venv/bin/activate
    
    Activate the virtual environment.
  • Deactivate the virtual environment

     % deactivate
    
    Deactivate the virtual environment.

And that’s it! Hack away.

Get the Complete Code of Python Code Snippets on Github.

Connect with me.

Need an engineer on your team to grease an idea, build a great product, grow a business or just sip tea and share a laugh?