A
cd ..
Tools

Python Virtual Environments

Manage isolated Python environments with venv, virtualenv, and conda.

2025-11-19
python, venv, virtualenv

Built-in venv

Create virtual environment

python3 -m venv myenv
python3 -m venv ~/envs/project1

Activate

# Linux/macOS
source myenv/bin/activate

# Windows
myenv\Scripts\activate

Deactivate

deactivate

Install packages

# After activation
pip install requests
pip install -r requirements.txt

Export dependencies

pip freeze > requirements.txt

Virtualenv (older tool)

Install

pip install virtualenv

Create environment

virtualenv myenv
virtualenv -p python3.9 myenv

Usage (same as venv)

source myenv/bin/activate
pip install package
deactivate

Conda

Install Miniconda

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Create environment

conda create -n myenv python=3.9
conda create -n dataenv python=3.9 pandas numpy

Activate

conda activate myenv

Deactivate

conda deactivate

List environments

conda env list
conda info --envs

Remove environment

conda env remove -n myenv

Export environment

conda env export > environment.yml

Create from file

conda env create -f environment.yml

Pipenv

Install

pip install pipenv

Create environment & install

pipenv install requests
pipenv install --dev pytest  # Dev dependency

Activate shell

pipenv shell

Run command

pipenv run python script.py

Lock dependencies

pipenv lock

Poetry

Install

curl -sSL https://install.python-poetry.org | python3 -

New project

poetry new myproject
cd myproject

Init existing project

poetry init

Add dependency

poetry add requests
poetry add --dev pytest

Install dependencies

poetry install

Run scripts

poetry run python script.py

Best practices

Project structure

myproject/
├── venv/           # Virtual environment
├── src/            # Source code
├── tests/          # Tests
├── requirements.txt # Dependencies
└── README.md

requirements.txt

requests==2.28.1
pandas>=1.4.0,<2.0.0
numpy~=1.23.0

Multiple environments

# Development
python3 -m venv venv-dev
source venv-dev/bin/activate
pip install -r requirements-dev.txt

# Production
python3 -m venv venv-prod
source venv-prod/bin/activate
pip install -r requirements.txt

Common commands

# Upgrade pip
pip install --upgrade pip

# List installed packages
pip list
pip freeze

# Show package info
pip show requests

# Uninstall package
pip uninstall requests

# Install specific version
pip install requests==2.28.1

# Install from git
pip install git+https://github.com/user/repo.git

Troubleshooting

# Can't activate?
ls -la venv/bin/activate
chmod +x venv/bin/activate

# Wrong Python version?
which python
python --version

# Clear pip cache
pip cache purge

# Reinstall all
pip install --force-reinstall -r requirements.txt

pyenv (Python version management)

# Install pyenv
curl https://pyenv.run | bash

# List available versions
pyenv install --list

# Install Python version
pyenv install 3.9.10

# Set global version
pyenv global 3.9.10

# Set local version
pyenv local 3.8.12

# Create venv with specific Python
pyenv virtualenv 3.9.10 myproject
pyenv activate myproject

Quick reference

# Create and activate
python3 -m venv venv && source venv/bin/activate

# Install from requirements
pip install -r requirements.txt

# Save dependencies
pip freeze > requirements.txt

# Deactivate
deactivate

# Remove venv
rm -rf venv

Was this useful?

Share with your team

Browse More