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

4 2 2 2 2

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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay