Skip to content
Robin te Hofstee — Personal Blog
➜~neofetch

robin@blog

Name
Robin te Hofstee
Role
Full-stack developer, sysadmin & technical writer
Focus
homelab · Linux · self-hosting · Next.js
Posts
61 · latest Nov 30, 2026
Host
blog.robintehofstee.com
Links
github · rss · portfolio
➜curlblog.robintehofstee.com
➜~cat/blog/setting_up_python_virtual_environments.mdx
←cd ~

Setting Up Python Virtual Environments

Oct 16, 20263 minPythoncoding

Setting Up Python Virtual Environments#

Introduction#

When working with Python projects, it's essential to keep dependencies organized and isolated from one another. This is where Python virtual environments come in. A virtual environment is a tool that creates an isolated environment for a project, allowing you to manage dependencies without affecting other projects or the global Python installation.

In this tutorial, we'll walk through the steps to set up and manage Python virtual environments using venv, virtualenv, and venvwrapper. We'll cover the basics of creating and activating environments, installing dependencies, and managing environment configurations.

Why Use Virtual Environments?#

  1. Isolation: Each project can have its own set of dependencies, preventing conflicts between different projects.
  2. Reproducibility: You can easily recreate the environment on another machine or share it with others.
  3. Simplicity: Keeps your global Python installation clean and uncluttered.

Setting Up venv#

Installing venv#

venv is a built-in module in Python 3.3 and later, so no additional installation is required. However, if you're using an older version, you might need to upgrade to Python 3.3 or later.

Creating a Virtual Environment#

Let's create a new virtual environment for a project. Open your terminal and navigate to the project directory. Run the following command:

sh
python3 -m venv my_project_env

This command creates a new directory called my_project_env (you can name it anything you like) containing the virtual environment.

Activating the Virtual Environment#

To activate the virtual environment on Unix or MacOS, run:

sh
source my_project_env/bin/activate

On Windows, use:

sh
my_project_env\Scripts\activate

Once activated, your terminal prompt should indicate that you're inside the virtual environment. You can now install dependencies and run Python scripts.

Setting Up virtualenv#

Installing virtualenv#

If you prefer using virtualenv, you can install it via pip:

sh
pip install virtualenv

Creating and Activating a Virtual Environment#

Create a new virtual environment with virtualenv:

sh
virtualenv my_project_env

To activate the environment on Unix or MacOS:

sh
source my_project_env/bin/activate

On Windows:

sh
my_project_env\Scripts\activate

Using venvwrapper#

Installing venvwrapper#

venvwrapper is a tool for managing multiple virtual environments. Install it using pip:

sh
pip install virtualenvwrapper-win

Setting Up venvwrapper#

Add the following lines to your ~/.bashrc or ~/.zshrc file (for Unix/MacOS) or ~/_rc file (for Windows):

sh
export WORKON_HOME=$HOME/.virtualenvs
source /path/to/virtualenvwrapper.sh

Replace /path/to/virtualenvwrapper.sh with the actual path to the script.

Reload your shell configuration:

sh
source ~/.bashrc

or

sh
source ~/.zshrc

or

sh
virtualenvwrapper-win init

Creating and Managing Environments with venvwrapper#

Create a new environment:

sh
mkvirtualenv my_project_env

List all environments:

sh
lsvirtualenv

Activate an environment:

sh
workon my_project_env

Installing Dependencies#

Once your virtual environment is set up, you can install dependencies using pip. For example, to install the requests library:

sh
pip install requests

You can also create a requirements.txt file to manage dependencies more systematically. After installing dependencies, you can freeze them:

sh
pip freeze > requirements.txt

To install all dependencies from requirements.txt, run:

sh
pip install -r requirements.txt

Managing Environment Configurations#

To deactivate your virtual environment and return to the global Python environment, simply type:

sh
deactivate

Conclusion#

Using virtual environments is a best practice for Python development, ensuring that your projects are isolated, reproducible, and maintainable. Whether you're using venv, virtualenv, or venvwrapper, you now have the tools to manage your Python projects effectively. Happy coding!


Share