Python > Deployment and Distribution > Dependency Management > Virtual Environments (`venv`, `conda`)
Creating and Using a Virtual Environment with `venv`
This snippet demonstrates how to create and activate a virtual environment using Python's built-in `venv` module. Virtual environments isolate project dependencies, preventing conflicts between different projects.
Creating a Virtual Environment
This command creates a new virtual environment in a directory named `.venv`. It uses the `venv` module, which is part of the standard Python library. The `.venv` directory will contain a copy of the Python interpreter and the `pip` package manager.
python3 -m venv .venv
Activating the Virtual Environment (Linux/macOS)
This command activates the virtual environment on Linux and macOS systems. Activating the environment modifies your shell's `PATH` variable so that when you run `python` or `pip`, you're using the versions within the virtual environment.
source .venv/bin/activate
Activating the Virtual Environment (Windows)
This command activates the virtual environment on Windows systems. The activation script is located in the `Scripts` directory within the virtual environment folder.
.venv\Scripts\activate
Installing Packages
After activating the environment, you can install project dependencies using `pip`. This command installs the `requests` and `flask` packages into the virtual environment. These packages are now available for use in your project without affecting the global Python installation.
pip install requests flask
Deactivating the Virtual Environment
When you're finished working on the project, you can deactivate the virtual environment. This removes the virtual environment's path from your shell's `PATH` variable, restoring the global Python environment. Simply type `deactivate` in your terminal.
deactivate
Why use Virtual Environments?
Virtual environments are crucial for managing dependencies in Python projects. They isolate project-specific packages, preventing conflicts that can arise when different projects require different versions of the same package. This ensures that your project has a consistent and predictable environment across different machines and deployments.
Real-Life Use Case
Imagine you have two projects: Project A, which uses Flask version 1.x, and Project B, which uses Flask version 2.x. Without virtual environments, installing Flask 2.x globally would break Project A. With virtual environments, each project can have its own Flask version, preventing conflicts and ensuring that both projects work correctly.
Best Practices
Interview Tip
Be prepared to explain the purpose of virtual environments and how they solve dependency management problems. Mention the `venv` and `conda` tools, and be able to demonstrate how to create and activate an environment.
When to use them
Use virtual environments for every Python project, regardless of size. It's good practice to isolate dependencies from the start, even if your project initially has very few dependencies.
Memory footprint
Virtual environments do take up some disk space, as they contain a copy of the Python interpreter and installed packages. However, the benefits of isolation and dependency management far outweigh the relatively small cost of storage.
Alternatives
While `venv` is Python's standard tool, other options exist, such as `conda`, `virtualenv`, and `pipenv`. `conda` is particularly useful for managing non-Python dependencies (e.g., system libraries) and creating reproducible environments across different operating systems.
Pros of `venv`
Cons of `venv`
FAQ
-
Why is the virtual environment directory often named `.venv`?
Using a dot `.` at the beginning of the directory name makes it a hidden directory in Linux/macOS systems, which helps to keep the project directory clean. It's a convention, but you can name it anything you like. -
How do I manage project dependencies in a virtual environment?
Use `pip freeze > requirements.txt` to generate a `requirements.txt` file containing a list of all installed packages and their versions. You can then install these dependencies on another machine using `pip install -r requirements.txt`.