DEV Community

Cover image for Python Virtual Environments: Why You Need Them and How to Use Them
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Python Virtual Environments: Why You Need Them and How to Use Them

If you’re diving into Python development, chances are you’ve heard whispers of something called "virtual environments" or simply "venv."

At first, it might sound a bit mysterious, but trust me, this little tool can become your best friend.

Let’s break it all down and see why it’s essential, how to use it, and the possibilities it unlocks.

What Is a Virtual Environment (venv)?

A virtual environment is like your personal workspace for a Python project. It’s an isolated environment where you can:

  • Install project-specific packages without messing up your global Python installation.
  • Use different versions of the same package across projects (because compatibility issues are a developer’s nightmare).
  • Keep your project dependencies clean and organized.

Imagine you’re working on two Python projects:

  1. Project A needs Django 4.0.
  2. Project B needs Django 3.2.

Without virtual environments, installing both versions on the same machine would result in chaos.

With virtual environments, you can switch between them seamlessly!

Image description
source

How to Set Up and Activate venv in Linux

Ready to set up your first virtual environment? Follow these steps:

1. Install venv

Most Python installations include venv, but if not, install it using:

sudo apt install python3-venv
Enter fullscreen mode Exit fullscreen mode

2. Create a Virtual Environment

Go to your project directory and create a virtual environment:

python3 -m venv venve
Enter fullscreen mode Exit fullscreen mode

Here, venv is the name of your virtual environment.

You can call it whatever you like.

3. Activate the Virtual Environment

To start using the virtual environment, activate it:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

You’ll know it’s activated because your terminal prompt will change to something like:

(venv) lovestaco@i3nux-mint:~$
Enter fullscreen mode Exit fullscreen mode

4. Install Deps

Now you can install packages specific to your project:

pip install flask
Enter fullscreen mode Exit fullscreen mode

6. Deactivate the Environment

When you’re done, deactivate the environment:

deactivate
Enter fullscreen mode Exit fullscreen mode

And you’re back to your system Python!

What Can You Do with venv?

The possibilities are endless! Here are a few ideas:

  • Experiment with Libraries: Try out new libraries or frameworks without worrying about breaking your system setup.
  • Version Testing: Test your code with different versions of Python or dependencies.
  • Collaborate Easily: Share a requirements.txt file with your team so everyone can set up the same environment.
  pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Your teammates can recreate the environment with:

  pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  • Deploy Projects: Virtual environments make it easy to deploy your projects to servers or containers.

Wrapping Up

Virtual environments are a simple yet powerful tool that every Python developer should use.

With just a few commands, you can create, activate, and manage isolated Python environments tailored to your projects.

So the next time you start a Python project, remember to fire up a virtual environment.

I’ve been working on a super-convenient tool called LiveAPI.

LiveAPI helps you get all your backend APIs documented in a few minutes

With LiveAPI, you can quickly generate interactive API documentation that allows users to execute APIs directly from the browser.

Image description

If you’re tired of manually creating docs for your APIs, this tool might just make your life easier.

Top comments (0)