Virtual Environment

The use of a Virtual Environment is to test python code in encapsulated environments, and to also avoid filling the base Python installation with libraries we might use for only one project.

venv

venv is the standard library module for creating virtual environments in Python 3.3+. It’s built into Python, so no installation is required.

  1. Create a virtual environment
python -m venv venv

Or on some systems:

python3 -m venv venv

This creates a venv directory in your current folder containing the virtual environment.

Quiz

Sign in to answer this quiz and track your learning progress

What command is used to create a virtual environment using venv?
A. python -m venv venv
B. python create venv
C. venv create
D. python venv new
  1. Activate the virtual environment

On Linux/macOS:

source venv/bin/activate

On Windows:

venv\Scripts\activate

Once activated, you’ll see (venv) at the beginning of your command prompt, indicating the virtual environment is active.

Quiz

Sign in to answer this quiz and track your learning progress

How do you activate a virtual environment on Linux/macOS?
A. activate venv
B. source venv/bin/activate
C. venv activate
D. python venv activate
  1. Install packages

With the virtual environment activated, install packages using pip:

pip install package_name

Packages installed will be specific to this virtual environment.

  1. Deactivate the virtual environment

To exit the virtual environment:

deactivate

The (venv) prefix will disappear from your command prompt.

Quiz

Sign in to answer this quiz and track your learning progress

What is the main purpose of using a virtual environment?
A. To isolate project dependencies and avoid filling the base Python installation
B. To make Python run faster
C. To encrypt Python code
D. To compile Python to machine code

virtualenv

  1. Install virtualenv
pip install virtualenv
  1. Install virtualenvwrapper-win (Windows)
pip install virtualenvwrapper-win

Usage:

  1. Make a Virtual Environment named HelloWorld
mkvirtualenv HelloWorld

Anything we install now will be specific to this project. And available to the projects we connect to this environment.

  1. Set Project Directory

To bind our virtualenv with our current working directory we simply enter:

setprojectdir .
  1. Deactivate

To move onto something else in the command line type deactivate to deactivate your environment.

deactivate

Notice how the parenthesis disappear.

  1. Workon

Open up the command prompt and type workon HelloWorld to activate the environment and move into your root project folder

workon HelloWorld

Poetry

From Poetry website

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

  1. Install Poetry
pip install --user poetry
  1. Create a new project
poetry new my-project

This will create a my-project directory:

my-project
├── pyproject.toml
├── README.rst
├── poetry_demo
│   └── __init__.py
└── tests
    ├── __init__.py
    └── test_poetry_demo.py

The pyproject.toml file will orchestrate your project and its dependencies:

[tool.poetry]
name = "my-project"
version = "0.1.0"
description = ""
authors = ["your name <your@mail.com>"]

[tool.poetry.dependencies]
python = "*"

[tool.poetry.dev-dependencies]
pytest = "^3.4"
  1. Packages

To add dependencies to your project, you can specify them in the tool.poetry.dependencies section:

[tool.poetry.dependencies]
pendulum = "^1.4"

Also, instead of modifying the pyproject.toml file by hand, you can use the add command and it will automatically find a suitable version constraint.

poetry add pendulum

To install the dependencies listed in the pyproject.toml:

poetry install

To remove dependencies:

poetry remove pendulum

For more information, check the documentation or read here:

Pipenv

From Pipenv website

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.

  1. Install pipenv
pip install pipenv
  1. Enter your Project directory and install the Packages for your project
cd my_project
pipenv install <package>

Pipenv will install your package and create a Pipfile for you in your project’s directory. The Pipfile is used to track which dependencies your project needs in case you need to re-install them.

  1. Uninstall Packages
pipenv uninstall <package>
  1. Activate the Virtual Environment associated with your Python project
pipenv shell
  1. Exit the Virtual Environment
exit

Find more information and a video in docs.pipenv.org.

Anaconda

Anaconda is another popular tool to manage python packages.

Where packages, notebooks, projects and environments are shared. Your place for free public conda package hosting.

Usage:

  1. Make a Virtual Environment
conda create -n HelloWorld
  1. To use the Virtual Environment, activate it by:
conda activate HelloWorld

Anything installed now will be specific to the project HelloWorld

  1. Exit the Virtual Environment
conda deactivate

UV

From UV Documentation

UV is an extremely fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools workflows. UV is 10-100x faster than pip and provides unified package management, virtual environment creation, and Python version management.

  1. Install UV
# Using curl (Linux/macOS)
curl -LsSf https://astral.sh/uv/install.sh | sh

# Using pip or pipx
pip install uv
  1. Create a new project with virtual environment
uv init my-project
cd my-project
  1. Add dependencies
uv add requests
  1. Run commands in project environment
uv run python script.py
  1. Activate the virtual environment manually (optional)
source .venv/bin/activate  # Linux/macOS
.venv\Scripts\activate     # Windows

UV automatically manages virtual environments, Python versions, and dependencies with exceptional speed and convenience.