How To Create A Virtual Environment In Python
When programming in Python, managing dependencies is the secret to avoiding conflicts and making sure that your projects run smoothly. One of the best ways to handle dependencies is by using Python virtual environments. If you’re new to this concept, don’t worry! This post will break it down for you, step by step.
What Is A Virtual Environment?
A virtual environment (or virtualenv) is a self-contained directory that contains all the necessary dependencies and libraries to run a Python project. It allows you to:
-
Isolate dependencies: Different projects can have different requirements without conflicting with each other.
-
Control Python versions: You can use a specific version of Python for a project without changing your global Python setup.
-
Prevent conflicts: If one project needs
requests==2.20.0
and another needsrequests==2.25.0
, virtual environments make this possible without ruining a other projects.
Why Use Virtual Environments?
-
No Dependency Conflicts: Different projects might require different versions of a package. Using virtual environments makes each project have its own version of libraries, avoiding version clashes.
-
Cleaner Setup: Your global Python environment stays clean, as dependencies for each project are stored in isolated directories.
-
Easy Collaboration: When you share a project, you can also share a
requirements.txt
file which makes it easy for others to recreate the environment you’ve set up
How To Setup A Virtual Environment?
Now setting up a virtual environment is not that hard and very simple. So let’s quickly go through the process of setting it up.
Step 1: Installing Python
Before you can create a virtual environment, you must make sure Python is installed on your computer. To install Python, you can download it from python.org or follow the post “Learn How To Install Python On Your Computer In 3 Minutes”
Step 2: Creating a virtual environment
Once you have installed Python, open your terminal or command prompt if you’re on Windows and run the following command:
python3 -m venv myenv
After, you should see a new folder in your directory with the name “myenv”. You could literally choose any other name, but for this example, I’ll be using “myenv”. Now before we can start using our environment, we have to activate it first.
Step 3: Activating our virtual environment
Now to activate the environment, run the following according to your operating system…
Mac/Linux
source myenv/bin/activate
Windows
.\myenv/Scripts/activate
Once activated, you’ll notice the virtual environment’s name is in your terminal prompt, indicating that you’re working within the isolated environment.
How to Install Libraries in a Virtual Environment?
Now that your virtual environment is active, you can install libraries using pip
, just like you normally would.
python3 -m pip install requests
This installs the requests
library specifically for the current virtual environment, without affecting your global Python setup.
Deactivate the Virtual Environment
Once you’re done working in your virtual environment, you can deactivate it by running:
deactivate
This returns you to the global Python environment.
Managing Dependencies with “requirements.txt”
To make it easy for others to replicate your environment, you can freeze the list of installed packages with:
pip freeze > requirements.txt
This creates a requirements.txt
file that lists all the packages and their versions. To install the dependencies listed in requirements.txt
in another environment, use
pip install -r requirements.txt
Best Practices for Virtual Environments
-
Always create a new virtual environment for each project: This keeps dependencies specific to that project and avoids version conflicts.
-
Use
requirements.txt
: When sharing your project or collaborating, always include therequirements.txt
file. -
Activate the virtual environment every time you work on your project: This ensures you’re using the correct set of dependencies.
Conclusion
Python virtual environments are an essential tool for any Python developer, helping you isolate your projects, manage dependencies, and avoid conflicts between packages. Whether you’re working solo or collaborating with others, virtual environments make life easier and your development process smoother.
0 Comments