Skip to main content

How to Run Multiple Versions of Python at One Time

Tags

How to Run Multiple Versions of Python at One Time

If you have ever wanted to compile and install multiple versions of Python side by side on the same Linux or Mac machine then this tutorial will walk you through how to set that up.  One reason you might want to run different versions of Python at the same time is to test out a specific implementation or test different environments against a specific feature.  Thankfully, running multiple versions of Python at the same time can be setup with relatively few steps.  Let's get started!

 

Note: This tutorial will examine downloading and compiling the source for Python 2.7.13 and Python 3.6.1 and will not be using a pre-built source installer that is found on python.org.  This can ultimately be done with any version of Python and is not specific to Python 2.7.13 or 3.6.1.  It should also be noted that this tutorial is aimed at an unix (Linux or Mac) based operating system and has not been tested on the Windows subsystem.  If your Window's machine has a Linux subsystem, in theory this tutorial should work because you have gcc available, but I have not tested it.

Prerequisites:

  • ​Mac: You will need an Xcode toolchain installed to take advantage of cmake for compiling Python.  Navigate out to the Mac App Store and download Xcode.
  • Linux: No prerequisites.  Just open a terminal and type gcc -v to make sure you have access to gcc.
  • You will need access to the terminal for this tutorial and you should have a basic understanding of Bash.

 

1) Download the Gzipped Tarballs for all Versions of Python

Python Version Download

First thing that needs to be done is to navigate out to the download page on Python.org and to download the Gzipped tarball for all versions of Python you are looking for.

Python 2.7

In this tutorial I am downloading the source for Python 2.7.13 and Python 3.6.1.  Download the version of Python you will want to test and save the Gzipped tarball to a location on your computer where you can easily access the downloaded files and test your code.

 

2) Extract the Tarballs

Extracted Tarball

Next, extract the tarball by double clicking the file if you are on a Mac or by running the tar extraction command below if you are on Linux.  This command should now allow you to have a working source tree to compile.

Matts-MBP:How to Run Multiple Versions of Python matteaton$ tar -xvzf Python-2.7.13.tgz

3) Compile the Python Source

Configure

After the source has been extracted, you now need to open up the terminal and navigate into the source directory of where you extracted the Python source. From here you will want to type the ./configure command if you are on a Linux machine and ./configure --prefix=/path/to/install/and/test MACOSX_DEPLOYMENT_TARGET=10.13 if you are using a Mac.  Making sure to replace the --prefix path with the place you are wanting the binary to be installed once it is compiled and the MACOSX_DEPLOYMENT_TARGET is set to your macOS version. 

The ./configure command will scan your operating system and configure the source in preparation to be compiled and installed.

# For Linux
Matts-MBP:Python-2.7.13 matteaton$ ./configure
 
# For Mac
Matts-MBP:Python-2.7.13 matteaton$ ./configure --prefix=/path/to/install/and/test MACOSX_DEPLOYMENT_TARGET=10.13
Make

Next is the compilation step.  If all went well with the configure step you should now be good to run the make command from the same directory.  This will compile the source files into a single Python binary if on Linux, and at this point you are ready to roll.  If you are on Mac, running the make command with compile the source files and setup the source files to be installed with the next step.

Matts-MBP:Python-2.7.13 matteaton$ make

4) Install the Python Source (Mac Only)

Make Install

If you are on a Mac you have already setup the configuration with an installation path described above.  Now you will need to install the compiled binary to the final location.  From the command line run 'make install' command to run the installation scripts and generate your Python source binary.  The final output of the 'make install' command might bury the Python binary in a /bin folder in the installation path you declared above.  Grab that Python binary by copying this file to your desired location on your machine.

Matts-MBP:Python-2.7.13 matteaton$ make install

If on macOS you are hit with an error during the 'make install' step about zlib packages not being installed you can install the Xcode developer tools to gain access to these missing packages by running this command below and then running 'make install' again.

$ xcode-select --install
$ make install

5) Test Final Output

Binary Output
Terminal Output

The final output should contain two Python binaries side-by-side that can now be executed independently from the global Python version or each other.  This should now get you started in testing multiple different versions of Python at one time!  Congratulations!

 

Alternative Approaches

An alternative option to consider before compiling Python source is to use Python Virtual Environments.  Using Virtual Environments allows you to setup multiple environments to install packages in and setup broader test cases than simply using the Python Standard Library.

Credits: Cover image designed by Freepik.

Member for

3 years
Matt Eaton

Long time iOS and server side team lead with a love for Python, Swift, ObjC, C, C++, networking, testing, network debugging, embedded development, technical writing, and research.

Comments

anushri

Mon, 03/26/2018 - 09:59 AM

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.