How to Install Multiple Versions of Python?

Developers often need to work with different versions of Python for various projects. Our project also had the same requirements. In this tutorial, I will show you how to install multiple versions of Python.

To install multiple versions of Python on Windows 11, first download the desired Python installers from the Python Downloads page. Run each installer, ensuring you check “Add Python to PATH” and customize installation settings to avoid conflicts. Verify installations using the Command Prompt with commands like py -3.8 --version and manage versions using the py launcher to switch between them easily.

Why Install Multiple Versions of Python?

There are several reasons why you might need multiple versions of Python on your system:

  1. Compatibility: Some projects may require a specific version of Python to run correctly.
  2. Testing: Developers often need to test their code against different Python versions to ensure compatibility.
  3. Learning and Experimentation: Experimenting with new features in the latest Python release while maintaining a stable environment for existing projects.

Install Multiple Versions of Python

Now, let me show you how to install multiple versions of Python on a Windows machine.

Step 1: Download Python Installers

First, download the installers for the Python versions you want to install. The official Python installers are on the Python Downloads page.

  1. Navigate to the Downloads page: Open your web browser and go to the Python Downloads page.
  2. Select the desired versions: Choose the versions you want to install (e.g., Python 3.8, Python 3.9, and Python 3.10).
  3. Download the installers: Click on the respective links to download the executable installers for each version.

Step 2: Install the First Version of Python

  1. Run the Installer: Locate the downloaded installer for the first version of Python (e.g., python-3.8.10-amd64.exe) and double-click to run it.
  2. Customize Installation: On the first screen, check the box that says “Add Python to PATH” and then click on “Customize installation”.
  3. Select Optional Features: Ensure that the following options are checked:
    • Documentation
    • pip
    • tcl/tk and IDLE
    • Python test suite
    • py launcher
  4. Advanced Options: In the Advanced Options screen, check the following:
    • Install for all users
    • Add Python to environment variables
    • Precompile standard library
  5. Install: Click on “Install” to begin the installation. Once the installation is complete, close the installer.

Step 3: Install Additional Versions of Python

Repeat the installation process for each additional version of Python, customizing the installation settings as described in Step 2. To avoid conflicts, it’s crucial to ensure that “Add Python to PATH” is checked for each version.

Step 4: Verify Installations

After installing all desired versions of Python, you can verify the installations using the Command Prompt.

  1. Open Command Prompt: Press Win + R, type cmd, and press Enter.
  2. Check Python Versions: Type the following commands to check the installed versions:
python --version
python3 --version
py -3.8 --version
py -3.9 --version
py -3.10 --version

Each command should return the version number of the respective Python installation.

Step 5: Manage Multiple Versions with py Launcher

The py launcher allows you to switch between different Python versions easily. It is installed by default with Python on Windows.

  • Specify Version: You can specify which version of Python to use by adding the version number after the py command. For example:
py -3.8 script.py
py -3.9 script.py
py -3.10 script.py
  • Set Default Version: To set a default Python version, you can create a .py file association. Open Command Prompt and type:
py -3.9 --default

When you run without specifying a version, this sets Python 3.9 as the default version.

Step 6: Using Virtual Environments

Virtual environments are essential for managing dependencies and avoiding conflicts between different projects. They allow you to create isolated environments for each project, each with its own Python version and packages.

  • Create a Virtual Environment: Open Command Prompt and navigate to your project directory. Then, run:
py -3.9 -m venv myenv

This command creates a virtual environment named myenv using Python 3.9.

  • Activate the Virtual Environment: To activate the virtual environment, run:
myenv\Scripts\activate
  • Deactivate the Virtual Environment: When you’re done working in the virtual environment, you can deactivate it by simply typing:
deactivate

Step 7: Install Packages in Virtual Environments

Once the virtual environment is activated, you can install packages using pip without affecting the global Python installation.

  • Install a Package: For example, to install the requests package, run:
pip install requests
  • List Installed Packages: To list the installed packages in the virtual environment, run:
pip list

Step 8: Manage Multiple Virtual Environments

You can create multiple virtual environments for different projects, each using a different Python version. Simply repeat the steps in Step 6 for each project, specifying the desired Python version with the py command.

Step 9: Updating Python Versions

When a new version of Python is released, you can install it alongside your existing versions. Follow the same steps as described in Step 2 to download and install the new version. Then, update your virtual environments to use the new version if needed.

  1. Install the New Version: Download and install the new Python version from the Python Downloads page.
  2. Update Virtual Environment: To update an existing virtual environment to use the new Python version, recreate the environment with the new version:
py -3.11 -m venv myenv

Conclusion

In this tutorial, I have explained how to install multiple versions of Python in Windows OS.

You may also like:

Leave a Comment