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:
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
On MacOS
Use brew to manage your Python versions:
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.
Or for systems using pyenv:
Activating and Deactivating Environments
Once the virtual environment is created, it can be activated with:
or
To deactivate the virtual environment and return to your system's default Python version, simply use:
Verifying the Python Version
After activating the virtual environment, ensure you're using the correct 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:
List all installed packages within the virtual environment:
Summary Table
The below table summarizes the setup steps to manage different Python environments using virtualenv.
| Step No | Action | Command/Instructions |
| 1 | Install virtualenv | pip install virtualenv |
| 2 | Install Python versions | Use apt on Ubuntu or brew on MacOS
(brew install [email protected]) |
| 3 | Create virtual environment | virtualenv -p /path/to/pythonX.X myenv |
| 4 | Activate environment | source myenv/bin/activate
(Unix/MacOS),
.\myenv\Scripts\activate (Windows) |
| 5 | Verify Python version | python --version |
| 6 | Manage dependencies | pip install package_name |
| 7 | Deactivate environment | deactivate |
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-packageswhen 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
virtualenvcommand 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.

