Python Virtual Environments24 Jan 2025 / 6 minutes to read Elena Daehnhardt |
Introduction
Let’s focus on using Virtual Environments in Python3.
A virtual environment is an isolated environment where you can install Python packages without affecting your global Python installation or other projects.
This isolation helps:
- Manage dependencies: Each project can have its packages with specific versions, avoiding conflicts.
- Maintain project-specific configurations: keep settings and packages separate for different projects.
- Simplify collaboration: ensure all team members use the same environment and dependencies.
- Clean up easily: delete the environment folder to remove all project-specific packages.
Using Venv
Using virtual environments is good practice, especially as projects grow in complexity and require different libraries that may not be compatible. The Python library venv allows each project to have its customised environment.
Venv creates a virtual environment, a folder containing scripts and a link to the Python interpreter. It offers two main benefits:
- You can install project-specific libraries in isolation for better control.
- When sharing your project, use “pip freeze > requirements.txt” to create a list of necessary libraries, keeping your environment clean.
Creating a Virtual Environment
Here’s how to create a virtual environment:
- Open your terminal or command prompt.
- Navigate to your project directory.
-
Create the environment:
python3 -m venv .venv
This creates a directory named
.venv
(you can choose any name) containing a copy of the Python interpreter, pip, and other necessary files.
Activating the Environment
Before installing packages or running your project, you need to activate the environment:
-
On Linux/macOS:
source .venv/bin/activate
-
On Windows:
.venv\Scripts\activate
You’ll see the environment name in your terminal prompt, indicating it’s active.
Installing Packages
Now, you can use pip
to install packages within the environment. They will be isolated from your global installation.
Pip is Python’s package manager. It installs and manages Python packages from the Python Package Index (PyPI) or other repositories.
For instance, we can install the tiny web framework Flask:
pip install Flask
If you get a message that pip has to be updated, run the upgrade command:
python3 -m pip install --upgrade pip
To install packages from a requirements.txt
file:
pip install -r requirements.txt
The flag -r tells pip to read the dependencies from a file instead of specifying them individually in the command line.
The requirements.txt is a plain text file that typically contains a list of package names and their versions. This is so useful for sharing project dependencies with others.
Deactivating the Environment
When you’re finished working on your project, deactivate the environment:
deactivate
This returns you to your global Python environment.
Deleting a Virtual Environment
To delete a virtual environment, simply delete its directory. Make sure the environment is deactivated first.
rm -rf .venv
This action is irreversible, so ensure you don’t need the environment anymore.
Renaming a Virtual Environment
While there’s no direct command to rename an environment, you can achieve this by:
- Deactivating the environment.
-
Renaming the environment directory:
mv .venv new_env_name
- Updating your scripts or commands that activate the environment with the new name.
Example Workflow
Let’s say you’re starting a new web TODO app project with Flask:
- Create a project directory:
mkdir my_todo
- Navigate to the directory:
cd my_todo
- Create a virtual environment:
python3 -m venv .venv
- Activate the environment:
source .venv/bin/activate
- Install necessary packages:
pip install Flask
- Write your TODO app code.
- Deactivate the environment when done:
deactivate
Best Practices
My few tips on how to use the virtual environments:
- Use virtual environments for every Python project when appropriate. I also use Docker quite a lot :), so I don’t always have to use a virtual environment.
- Include a
requirements.txt
file to define the project dependencies. You can usepip freeze > requirements.txt
to generate it. - Consider using a tool like pipenv or poetry. These tools provide advanced features for managing dependencies and environments.
Personally, Docker and Pip work well for me. But it is your freedom to choose what best fits you.
Conclusion
Using virtual environments helps you manage Python projects better. It keeps your code clean and organised, avoids dependency problems, and makes collaboration easier.
Did you like this post? Please let me know if you have any comments or suggestions.
Posts about development tools and Python codingReferences
1. venv — Creation of virtual environments
About Elena Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.
|