Python > Deployment and Distribution > Dependency Management > Creating and Activating Virtual Environments
Creating and Activating Virtual Environments with `venv`
This snippet demonstrates how to create and activate virtual environments using Python's built-in `venv` module. Virtual environments are essential for managing dependencies in Python projects, ensuring that each project has its own isolated set of packages.
Creating a Virtual Environment
This command creates a new virtual environment named 'myenv' in the current directory. The `python3 -m venv` command invokes the `venv` module to create the environment. 'myenv' is the directory where the environment files will be stored.
python3 -m venv myenv
Activating the Virtual Environment (Linux/macOS)
This command activates the virtual environment on Linux and macOS systems. The `source` command executes the `activate` script within the 'myenv/bin' directory, modifying the shell's environment to use the environment's Python interpreter and packages.
source myenv/bin/activate
Activating the Virtual Environment (Windows)
This command activates the virtual environment on Windows systems. Similar to Linux/macOS, it executes the `activate` script, but the script is located in the 'myenv\Scripts' directory.
myenv\Scripts\activate
Installing Packages
After activating the virtual environment, you can install packages using `pip`. This example installs the 'requests' library. All packages installed while the environment is active will be installed within the environment, isolated from the system-wide Python installation.
pip install requests
Deactivating the Virtual Environment
To deactivate the virtual environment, simply run the `deactivate` command in your terminal. This reverts the shell's environment back to the system-wide Python installation.
deactivate
Concepts Behind the Snippet
Virtual environments solve dependency conflicts. Each project can have its own specific versions of packages without affecting other projects. This promotes reproducibility and prevents issues caused by incompatible library versions. `venv` is Python's recommended tool for managing virtual environments as it is included in the standard library.
Real-Life Use Case Section
Consider a scenario where you have two projects: Project A, which requires 'requests' version 2.20, and Project B, which requires 'requests' version 2.28. Without virtual environments, installing version 2.28 globally would break Project A. Virtual environments allow you to create a separate environment for each project, installing the appropriate version of 'requests' in each.
Best Practices
Interview Tip
Be prepared to explain why virtual environments are important, how they work, and the steps involved in creating, activating, and deactivating them. Mention `venv` and its advantages (being part of the standard library). Be ready to compare it with alternatives like `conda` or `pipenv`.
When to Use Them
Use virtual environments for virtually every Python project. They are especially crucial for collaborative projects, where consistent dependency management is essential. Use them also when deploying to production environments to ensure that your application has the correct dependencies.
Alternatives
Alternatives to `venv` include:
Pros
Cons
FAQ
-
Why should I use a virtual environment?
Virtual environments isolate project dependencies, preventing conflicts and ensuring that each project has the correct versions of packages it needs. This leads to more reliable and reproducible projects. -
What is the difference between `venv` and `virtualenv`?
`venv` is Python's standard library module for creating virtual environments, while `virtualenv` is a third-party library that provides similar functionality. `venv` is generally preferred as it's included with Python, eliminating the need for external installation. -
How do I make sure my dependencies are the same on my development and production servers?
Use `pip freeze > requirements.txt` to export a list of the packages and versions in your virtual environment on your development machine. Then, on your production server, activate the virtual environment and run `pip install -r requirements.txt` to install the exact same dependencies.