Creating Python Virtual Environments

Creating Python Virtual Environments


python virtualenv venv best-practices
Last updated on

Note (Updated 2025): This post was originally written in 2021. The method described here using venv is still the standard way to create virtual environments in Python today. Alternatives such as Poetry, Pipenv, and Conda may also be used depending on your workflow, but venv remains the simplest, built-in option.


Python applications often rely on packages and modules that don’t come with the standard library. Different applications may also need different versions of the same library. This can quickly cause conflicts:

  • App A requires version 1.0 of a library
  • App B requires version 2.0 of the same library

Installing both globally is impossible — one will break.

The solution is to create a virtual environment, a self-contained directory tree that has its own Python installation and packages, isolated from your system Python.


Creating a Virtual Environment

Choose a directory where you want to keep your environments:

$ mkdir Playground
$ cd Playground

Create a new environment named learning_env:

$ python3 -m venv learning_env

This creates a new directory with:

  • pyvenv.cfg — points to the Python installation used
  • lib/ — contains the Python version and site-packages for installed libraries
  • include/ — for compiling packages
  • bin/ (Unix/macOS) or Scripts/ (Windows) — contains the Python binary and activation scripts

Together, these isolate your project’s dependencies from the global system.

Activating the Environment

Activate the environment with:

$ source learning_env/bin/activate

Your shell prompt will now include the environment name:

(learning_env) $

Inside the environment, you can just use python or pip and pip (no need for python3 or pip3).

Using the Environment

Check the Python version:

(learning_env) $ python --version
Python 3.9.1

Install packages as usual:

(learning_env) $ pip install requests

Everything is installed inside learning_env, isolated from your global Python.

Deactivating

When you’re done, type:

(learning_env) $ deactivate
$ python --version
Python 2.7.16   # back to system default

Modern Alternatives (2025)

While venv remains reliable, developers today also use:

  • Poetry → dependency management + virtualenv + packaging in one
  • Pipenv → manages Pipfile and /Pipfile.lock automatically
  • Conda → great for data science, handles Python + non-Python packages

For most apps, though, the built-in venv is the simplest and most portable choice.


Wrap-up

Virtual environments solve the problem of conflicting dependencies by isolating projects. Whether you use venv, Poetry, or Conda, it’s a best practice to never develop Python projects in your system Python installation.

Quick Comparison

ToolBuilt-in?Best ForProsCons
venv✅ YesGeneral projects, simple setupsLightweight, no extra installs, universalNo dependency resolution, just isolation
Poetry❌ NoModern app development, packagingLockfile, publishing support, dependency managementExtra tool to install, opinionated
Pipenv❌ NoLegacy projects, Pipfile workflowIntegrates pip + virtualenv, human-friendly configPopularity declined, slower than Poetry
Conda❌ NoData science, ML, scientific computingHandles Python + native libs (e.g., BLAS)Heavyweight, separate ecosystem
© 2025 Syed Aslam