Managing Python Virtual Environments

How to manage Python virtual environments (venvs) for clean and isolated Python project setups.

About this post

  • Created 2025-10-23

  • Updated 2025-12-20

  • PythonVirtual Env

Intro

When working on Python projects, managing dependencies can quickly become messy. Virtual environments (venvs) let you isolate each project's packages, avoid version conflicts and keep your global Python installation clean. Using venvs also makes it easy to reproduce setups across different machines or share your project with others.

Create a venv

python3 -m venv myvenv

Activate a venv

Activating a venv is like entering the virtual environment. Inside the venv you install packages with pip and run your Python scripts.

source myvenv/bin/activate

Deactivate a venv

Deactivating means exiting your virtual environment.

deactivate

Delete a venv

deactivate
rm -r myvenv

Install from a file

The requirements.txt is a special file used to store the project's dependecies. It's like the package.json file for the npm ecosystem and can be pushed to GitHub to share it with others.

After activating the venv, run:

pip3 install -r requirements.txt

Freeze into a file

You don't have to create the requirements.txt manually. Instead, you can use the freeze command to export the packages into a file.

Once again, make sure you activated the venv before running the command:

python3 -m pip freeze > requirements.txt

Could be useful