How to create a virtual environment in Python
Imagine you are cooking in a shared kitchen where everyone uses the same ingredients and utensils. If one person replaces salt with sugar, it could ruin everyone’s dishes. A virtual environment is like having a separate kitchen where you store your ingredients and tools, ensuring no conflicts arise between different recipes.
A Python virtual environment is an isolated workspace that allows developers to install and manage project-specific dependencies without interfering with the system-wide Python installation. This ensures that different projects can use different versions of packages and dependencies without conflict.
When working with Python, a basic approach is to install Python on your machine, install all required libraries globally via the terminal, write your code, and execute it. This method works for simple scripting but can become problematic in larger software projects.
In development, suppose you are developing two Python projects; Project A requires Django version 1.0, and Project B requires Django version 2.0, which has breaking changes. If you install both packages globally, version conflicts can arise, causing errors in one or both projects. A virtual environment helps you avoid this issue by keeping dependencies isolated within each project.
Benefits of a Virtual Environment
- Isolates dependencies for each project, preventing conflicts.
- Allows multiple Python versions to coexist.
- Enables installing packages without admin privileges.
- Facilitates easy replication of environments for other developers.
- Supports creating a dependency file (requirements.txt) to install exact dependencies for a project.
How to Set up a Virtual Environment
Step 1: Create a directory (folder) for the project you want to work on.
Open your terminal and enter the "mkdir" command to make a directory. Or manually create your project directory (folder) on your computer and open it on Visual Studio code or any IDE of your choice, then directly use their terminal for the subsequent steps.
mkdir project_name
Step 2: Navigate to the directory
Use the change directory (cd) command to go into the desired directory. Simply type in the command cd <name-of-directory>
My directroy is named "techloytest" so I am changing the directory into that path.
Step 3: Create the virtual environment
Enter the command python -m venv env
in your terminal.
Your virtual environment can be named anything. But it is the convention to name it "env" or "venv". If you check, you should see the folder nicely created in your directory.
How to Activate Virtual Environment
On Command Prompt: env/Scripts/activate.bat
On Powershell: env/Scripts/Activate.ps1
On bash/zsh: env/bin/activate
On fish: env/bin/activate.fish
Enter the command for your machine in your terminal. Once activated, you should see the name of the environmnet next to your path information in the terminal.
How to Deactivate a Virtual Environment
All you have to do is type in "deactivate" and the virtual environment will be disabled.
Virtual Environment Best Practices
1/ Never move your virtual environment folder
If you created your virtual environment in the wrong directory, you may be tempted to move it to the right directory within the project. Well, DON'T! The reason for this is that the virtual environment contains absolute paths to the Python interpreter, libraries, and other dependencies, which can break when the environment is moved. Instead, delete the virtual environment folder, change the directory to the right one, then create the virtual environment again and reinstall your dependencies.
2/ Always gitignore your virtual environment
Virtual environments can be large and contain many files, which can slow down Git operations and increase repository size. By git ignoring the virtual environment, you avoid bloating your repository. The Virtual environments can also contain platform and system-specific files, which may not be compatible across different environments. To gitignore your virtual environment, create a .gitignore file, and add the virtual environment folder to it like this "env/"
Conclusion
Virtual environments are essential for managing dependencies and ensuring project stability in Python development. By isolating dependencies, they prevent conflicts and allow you to work on multiple projects simultaneously without affecting one another.
Using virtual environments also simplifies collaboration, as you can share a list of dependencies that other developers can easily install to replicate the same working environment, making project setup and maintenance much more efficient.
Incorporating virtual environments into your workflow helps maintain a clean and organized development setup. Whether you are building APIs, machine learning models, or web applications, using virtual environments will save you time and effort while reducing the risk of dependency-related issues.
Image credit: Oyinebiladou Omemu/techloy.com