Using Python 3 in virtualenv
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Virtual environments in Python are a cornerstone of Python development, enabling developers to manage dependencies and isolate projects effectively. virtualenv is a tool that creates isolated Python environments, allowing programmers to have multiple versions of Python libraries or even Python itself without interference. This is particularly useful in scenarios where different projects require different versions of libraries, or when system-wide installation of libraries is not feasible.
Understanding virtualenv
virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need. It can be used standalone, regardless of administrative permissions, and it supports Python 2 as well as Python 3.
Installation
Firstly, ensure that Python and pip (Python's package installer) are installed on your system. virtualenv can be installed globally using pip:
Basic Usage
To create a virtual environment, you need to specify a path where the environment should be located. For example, to create an environment called env:
This command will create a directory env in your current directory with a fresh, isolated Python installation. To start using this environment, you must activate it:
- On Windows:
- On MacOS and Linux:
Once activated, the shell prompt will change to show the name of the activated environment. While activated, any package that you install using pip will be placed in the env directory, isolated from the global Python environment.
Deactivation
To stop using the virtual environment and return to the global Python environment, simply run:
Managing Packages
Any command that you can use with pip is available in your virtual environment. For example, to install the latest version of numpy within your activated environment:
Using Different Python Versions
virtualenv allows you to create environments with different Python versions. This is possible if you have multiple versions of Python installed on your system. You can specify the Python version at the time of creating your environment:
Here, -p flag is used to specify the Python interpreter.
Best Practices and Tips
- Use a Requirements File: For better reproducibility, you can keep a
requirements.txtfile in your project directory that lists all of your project’s dependencies. To install all the dependencies listed in this file:
- Version Control: It is a good practice not to include your virtual environment folder (
envin this case) in version control. Instead, just include therequirements.txt. - Multiple Environments: For complex projects, it may be suitable to maintain different environments for development and production to mimic different settings.
Summary Table
| Feature | Description |
| Isolation | Independent from global site-packages |
| Customization | Each environment can have its own set of installed packages and Python versions |
| Non-admin Installation | Can be installed and used without administrative rights |
| Portability | Easy to replicate the environment using requirements.txt |
| Usage | Activate with source env/bin/activate and deactivate with deactivate |
Conclusion
Using virtualenv to manage Python environments offers a robust, straightforward methodology for dependency management across multiple projects. It helps in ensuring that projects are isolated, reproducible, and that they do not interfere with each other. This makes virtualenv an invaluable tool for both development and production Python workflows.

