Back to Python
2026-02-266 min read

… work with multiple versions of Python installed in parallel?

Learn … work with multiple versions of Python installed in parallel? step by step with clear examples and exercises.

Title: Working with Multiple Versions of Python Installed in Parallel

Why This Matters

In a typical development environment, you may need to work with multiple versions of Python for different projects or testing purposes. This lesson will guide you through the process of managing and working with multiple versions of Python installed on your system using tools such as virtualenv, venv, and pyenv.

The Importance of Managing Multiple Versions

  • Ensures compatibility between different projects that require various Python versions.
  • Prevents conflicts and unexpected behavior due to conflicting dependencies or versions.
  • Allows for testing code against specific Python versions without affecting other projects.

Prerequisites

Before diving into this topic, make sure you are familiar with:

  • Basic Python syntax and concepts
  • Navigating the command line or terminal (Linux/macOS) or Command Prompt (Windows)
  • Installing Python packages using pip
  • Understanding the differences between Python versions (e.g., 2.x, 3.x)
  • Familiarity with Git and version control systems is beneficial but not required.

Core Concept

There are several tools available to help manage multiple versions of Python on your system:

  1. virtualenv: This allows you to create isolated Python environments for different projects, each with its own set of dependencies and packages.
  1. venv: A built-in Python module that creates virtual environments for a specific Python version. It's included in Python 3.3 and later versions.
  1. pyenv: An external tool to manage multiple Python installations in your project directory, including different versions of Python 2 and 3.

Understanding Virtual Environments

Virtual environments provide a way to isolate projects from each other and the system's Python installation. Each virtual environment has its own copy of Python, packages, and dependencies, allowing you to work on multiple projects without conflicts.

How Virtual Environments Work

  • When activated, the virtual environment takes precedence over the system's Python installation.
  • Packages installed within a virtual environment are isolated from other environments and the system.
  • Activating a virtual environment makes it easier to manage dependencies for specific projects.

Worked Example

Let's create two virtual environments for Python 3.7 and 3.8, install a package in each, and run a simple script:

  1. Create the virtual environments (using virtualenv):
virtualenv -p python3.7 venv_3.7
virtualenv -p python3.8 venv_3.8
  1. Activate each environment and install a package (e.g., requests):
source venv_3.7/bin/activate
pip install requests

...and repeat for venv_3.8.

  1. Write a simple script in each environment's scripts folder to print the Python version and run the requests package:

venv_3.7/scripts/example.py

import sys

import requests

print(sys.version)

response = requests.get('https://api.github.com')

print(response.json())


...and repeat for `venv_3.8`.

4. Run the scripts in each environment:

source venv_3.7/bin/activate

python scripts/example.py


...and repeat for `venv_3.8`. You should see different Python versions and responses from GitHub API, demonstrating that you're working with separate Python environments.

### Managing Dependencies within Virtual Environments

- Install packages using pip within the activated virtual environment.
- To list installed packages, use `pip freeze` or `pip list`.
- To create a requirements file (e.g., `requirements.txt`) with all installed packages and their versions, run `pip freeze > requirements.txt`.

Common Mistakes

  1. Not activating the virtual environment: Always activate the virtual environment before installing packages or running scripts within it.
  2. Installing packages globally: Make sure to run pip commands inside the activated virtual environment.
  3. Misunderstanding isolation: Each virtual environment is isolated, so packages installed in one won't be available in another unless you copy them manually.
  4. Not using venv or pyenv: While virtualenv is the standard tool for creating virtual environments, some users may still use the older virtualenvwrapper or other alternatives. Using venv or pyenv can provide better management and compatibility with different Python versions.
  5. Not cleaning up: Don't forget to delete unused virtual environments to keep your system organized.
  6. Ignoring package conflicts: Be aware of potential package conflicts when working with multiple versions of the same package in different virtual environments.
  7. Not using pipenv: Pipenv is another tool that combines package management, virtual environment creation, and dependency management into a single command. Some developers prefer it for its convenience, but understanding the basics of virtualenv, venv, and pyenv is still essential.

Practice Questions

  1. How can you list all active Python virtual environments on your system? (Hint: For virtualenv, check the ~/.virtualenvs/ directory.)
  2. What command would you run to install a package called my_package in the current activated virtual environment using pip?
  3. Suppose you have two projects, each requiring different versions of a specific Python package. How would you manage this using virtualenv or venv?
  4. If you want to use pyenv to manage your Python environments, how would you install a new version of Python and create a virtual environment for it?
  5. What is the purpose of the pip freeze command when working with virtual environments?
  6. How can you ensure that all developers on a team are using the same versions of packages in their virtual environments?
  7. Explain the difference between global installation and virtual environment installation of Python packages.
  8. What is the advantage of using pipenv over traditional virtualenv or venv for managing dependencies within virtual environments?
  9. How can you update all packages in a virtual environment to their latest versions using pip?
  10. What steps would you take to troubleshoot an issue with a package installation or dependency conflict within a virtual environment?

FAQ

  1. Why should I use virtualenv (or venv) instead of installing packages globally?

Using virtualenv or venv allows for better isolation and management of project dependencies, preventing conflicts between projects that require different versions of the same package.

  1. Can I have multiple versions of Python installed on my system at once?

Yes, you can install multiple versions of Python on your system, but managing them effectively is essential to avoid conflicts. This is where tools like virtualenv, venv, and pyenv come in handy.

  1. What's the difference between virtualenv and venv?

While both tools create isolated Python environments, virtualenv is a third-party tool while venv is a built-in Python module. virtualenv allows for more flexibility and compatibility with different Python versions, but venv is included by default in recent Python versions.

  1. What's the difference between virtualenv and virtualenvwrapper?

Virtualenvwrapper is an extension for virtualenv that provides additional features such as easier creation, activation, and deletion of virtual environments. Some users prefer it due to its convenience, but the standard virtualenv remains widely used.

  1. How do I manage my Python environments with pyenv?

Pyenv allows you to easily install, switch, and delete multiple versions of Python. You can find more information on how to use pyenv in their official documentation.

  1. What is the purpose of pipenv when compared to virtualenv or venv?

Pipenv combines package management, virtual environment creation, and dependency management into a single command, making it easier for developers to manage dependencies within their projects.

  1. How can I update all packages in a virtual environment to their latest versions using pip?

To upgrade all packages in a virtual environment to their latest versions, use the following command:

pip install -U .

This command upgrades all installed packages within the current activated virtual environment. If you want to update specific packages, list them instead of using the dot (.).

… work with multiple versions of Python installed in parallel? | Python | XQA Learn