Creating a Python Virtual Environment with venv
A virtual environment in Python is a self-contained directory that contains a specific Python version and related dependencies. This environment is separate from your system-wide Python installation, allowing for better project management and dependency control.
Why Use a Virtual Environment?
1. Dependency Isolation:
Each project can have its own set of libraries and dependencies, ensuring that they do not conflict with libraries from other projects. This isolation prevents version conflicts, where one project might require an older or newer version of the same library than another project.
2. Reproducibility:
By isolating dependencies, a virtual environment ensures that your project behaves the same on different machines or environments. It allows others (or future you) to replicate the exact setup you had when the project was initially developed.
3. Prevents Global Installation:
Without a virtual environment, packages are installed globally across all Python projects. This can cause conflicts, especially when working with multiple projects that need different versions of the same package.
4. Portability:
Virtual environments can be bundled and shared easily. Using tools like requirements.txt, you can list all dependencies, enabling others to replicate your environment on their machines effortlessly.
5. Project Organization:
Keeping each project’s dependencies within its own environment keeps the global Python environment clean and minimizes clutter. This leads to better project organization and ease of use.
What is venv?
The venv module is a built-in feature in Python (introduced in Python 3.3) that allows you to create and manage virtual environments. It creates a lightweight directory structure that includes the Python executable and libraries.
- Built-in: You do not need to install anything extra to use venv, as it is included with Python 3.3+.
- Portable: Virtual environments created with venv can be used across different operating systems (Linux, macOS, Windows), making your projects highly portable.
Steps to Create a Virtual Environment using venv
1. **Install venv (if not already installed)**
If you're using Python 3.3 or later, venv is included by default. However, you can verify its installation or install it if necessary.
- **For Windows**: venv comes with Python installations of 3.3+ by default.
- **For Debian/Ubuntu (Linux)**:
sudo apt-get install python3-venv
2. **Create a Virtual Environment**
Navigate to your project directory and run the following command to create a virtual environment using the venv module.
python -m venv <env_name>
- Replace <env_name> with the desired name for your virtual environment.
**Example**:
python -m venv myenv
This will create a directory named myenv that contains the virtual environment's files.
3. **Activate the Virtual Environment**
Once the environment is created, you need to activate it.
- **For Windows**:
.\myenv\Scripts\activate
- **For Linux/MacOS**:
source myenv/bin/activate
After activation, your shell prompt will change to reflect the active environment:
Recommended by LinkedIn
(myenv) user@host:~/your_project$ # in Debian/Ubuntu
# OR
(myenv) C:\path\to\your\project> # in Windows
4. **Deactivate the Virtual Environment**
To exit the virtual environment, simply use the command:
deactivate
This will return you to the global Python environment.
5. **Install Packages within the Virtual Environment**
Once the virtual environment is activated, you can install dependencies using pip, and they will be installed locally within the virtual environment.
pip install <package_name>
**Example**:
pip install requests
6. **List Installed Packages**
You can see the packages installed in your virtual environment using:
pip list
7. **Creating a requirements.txt file**
A requirements.txt file is commonly used in Python projects to list all the dependencies that your project needs. This allows you to easily install all required packages in a virtual environment.
Steps to create a requirements.txt file:
1. First, activate your virtual environment, the same as in [[#3. **Activate the Virtual Environment**]].
- **For Windows**:
.\myenv\Scripts\activate
- **For Linux/MacOS**:
source myenv/bin/activate
After activation, your shell prompt will change to reflect the active environment:
(myenv) user@host:~/your_project$ # in Debian/Ubuntu
OR
(myenv) C:\path\to\your\project> # in Windows
2. After installing all necessary packages in your virtual environment, generate the requirements.txt file by running:
pip freeze > requirements.txt
Resources
- [Python Documentation: venv](https://docs.python.org/3/library/venv.html)
- [Real Python Guide: Python Virtual Environments](https://realpython.com/python-virtual-environments-a-primer/)
- [Virtual Environments and Packages in Python - Official Docs](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/)
- [Venv vs Virtualenv: What’s the Difference?](https://pythonspeed.com/articles/venv-vs-virtualenv/)
- [How to Set Up a Python Virtual Environment on Windows, Mac, and Linux](https://www.freecodecamp.org/news/how-to-setup-virtual-environments-in-python/)
Amazing!