Python
virtualenv
Python versions
software development
programming tutorial

Use different Python version with virtualenv

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Using multiple Python versions on a single machine can be crucial for testing compatibility, dealing with legacy code, or leveraging new language features. The virtualenv tool simplifies this task, allowing developers to create isolated Python environments with different versions. This article details how to manage multiple Python versions using virtualenv.

Why Use virtualenv?

virtualenv is a tool that creates isolated Python environments. It helps in managing dependencies for different projects separately by constructing a bespoke environment with its own libraries and system paths. This is especially valuable in scenarios where:

  • Different projects require different versions of the same library.
  • Migrating a project to a different Python version is necessary.
  • Experimenting with a new Python feature without affecting the global environment.

Setting Up Multiple Python Versions

Installing Virtualenv

Before using virtualenv, ensure it's installed:

bash
pip install virtualenv

Installing Multiple Python Versions

In order to create a virtual environment with different Python versions, you must have those versions installed on your system.

On Ubuntu

bash
sudo apt update
sudo apt install python3.7 python3.8 python3.9

On MacOS

Use brew to manage your Python versions:

bash
brew install [email protected]
brew install [email protected]
brew install [email protected]

Creating Virtual Environments with Specific Python Versions

You can specify the Python interpreter when creating a virtual environment. This is done using the --python option pointing to the exact path of the Python binary.

bash
virtualenv -p /usr/bin/python3.8 myenv38

Or for systems using pyenv:

bash
virtualenv -p $(pyenv which python3.7) myenv37

Activating and Deactivating Environments

Once the virtual environment is created, it can be activated with:

bash
source myenv38/bin/activate  # On Unix or MacOS

or

bash
.\myenv38\Scripts\activate  # On Windows

To deactivate the virtual environment and return to your system's default Python version, simply use:

bash
deactivate

Verifying the Python Version

After activating the virtual environment, ensure you're using the correct Python version:

bash
(myenv38) $ python --version

This command should display Python 3.8.x reflecting the specified version during the creation of the virtual environment.

Managing Dependencies

Once a virtual environment is active, you can use pip to install project-specific dependencies without affecting other environments or the system's global Python:

bash
pip install requests

List all installed packages within the virtual environment:

bash
pip freeze

Summary Table

The below table summarizes the setup steps to manage different Python environments using virtualenv.

Step NoActionCommand/Instructions
1Install virtualenvpip install virtualenv
2Install Python versionsUse apt on Ubuntu or brew on MacOS (brew install [email protected])
3Create virtual environmentvirtualenv -p /path/to/pythonX.X myenv
4Activate environmentsource myenv/bin/activate (Unix/MacOS), .\myenv\Scripts\activate (Windows)
5Verify Python versionpython --version
6Manage dependenciespip install package_name
7Deactivate environmentdeactivate

Caveats and Considerations

  • Global Site Packages: By default, virtual environments do not include the system's global site packages. This can be altered using --system-site-packages when creating a virtualenv, though it is generally discouraged for dependency isolation.
  • Path Management: Specifying the exact path to the Python interpreter is crucial as the virtualenv command relies on it for establishing the correct version.
  • Environment Size: Each environment is a complete, self-contained instance of Python, thus can consume a significant amount of disk space, especially when numerous packages are installed.

Conclusion

virtualenv is an invaluable utility for managing python project dependencies and ensuring compatibility with various Python versions. By following best practices and using the right Python versions, developers can streamline their projects, ensuring consistency across different development environments.


Course illustration
Course illustration

All Rights Reserved.