Python virtual environments prevent package conflicts in Django projects. Creating one with the standard library’s venv
module gives each project its own interpreter and set of packages, so upgrades in one project don’t break another.
Method 1 — Use Python’s built‑in venv (recommended)
Step 1: Open a terminal or command prompt.
Step 2: Move into your project folder.
cd /path/to/your/project
Step 3: Create a virtual environment.
# macOS/Linux
python3 -m venv .venv
# Windows (PowerShell or CMD)
python -m venv .venv
Step 4: Activate the virtual environment.
# macOS/Linux (bash/zsh)
source .venv/bin/activate
# macOS/Linux (fish)
source .venv/bin/activate.fish
# macOS/Linux (csh/tcsh)
source .venv/bin/activate.csh
# Windows PowerShell
.\\.venv\\Scripts\\Activate.ps1
# Windows CMD
.\\.venv\\Scripts\\activate.bat
Step 5: Fix PowerShell execution policy if activation is blocked (Windows only).
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
Step 6: Confirm you’re using the venv’s Python.
# macOS/Linux
which python
# Windows PowerShell
Get-Command python
Step 7: Upgrade pip inside the venv.
python -m pip install --upgrade pip
Step 8: Install packages into the venv.
python -m pip install requests
Step 9: Optionally install from a requirements file.
python -m pip install -r requirements.txt
Step 10: Save exact package versions for reproducibility.
python -m pip freeze > requirements.txt
Step 11: Deactivate the venv when finished.
deactivate
Step 12: Keep the venv out of version control.
echo ".venv/" >> .gitignore
Notes: You can run a script with the venv’s interpreter directly (for example, ./.venv/bin/python app.py
) without activation. Virtual environments are disposable; if you move the project path, recreate the venv and reinstall from requirements.txt
.
Method 2 — Create and use a venv in Visual Studio Code
Step 1: Open your project folder in VS Code.
Step 2: Open the Command Palette.
# Keyboard shortcut
Ctrl+Shift+P (Windows/Linux)
Cmd+Shift+P (macOS)
Step 3: Run Python: Create Environment
.
Step 4: Choose venv
as the environment type.
Step 5: Select a Python interpreter (e.g., 3.11) when prompted.
Step 6: If you have one, pick requirements.txt
to auto-install dependencies.
Step 7: Select the interpreter from the Status Bar or run Python: Select Interpreter
and pick .venv
.
Step 8: Open a new integrated terminal to auto-activate the venv.
Step 9: Verify the interpreter path is inside .venv
.
# macOS/Linux
which python
# Windows PowerShell
Get-Command python
Step 10: Install packages in the terminal attached to the workspace.
python -m pip install "fastapi[standard]"
If activation fails in PowerShell, apply the execution policy from Method 1 (Step 5). If VS Code doesn’t discover your venv, use Python: Select Interpreter
and browse to .venv/Scripts/python.exe
on Windows or .venv/bin/python
on macOS/Linux.
Method 3 — Create a venv with the virtualenv package
Step 1: Install virtualenv
if you need it for legacy workflows.
python -m pip install --user virtualenv
Step 2: Create an environment using a specific Python.
# Use default python3 on macOS/Linux
virtualenv -p python3 venv
# Or point at an exact interpreter
virtualenv -p /full/path/to/python venv
Step 3: Activate the environment.
# macOS/Linux
source venv/bin/activate
# Windows PowerShell
.\\venv\\Scripts\\Activate.ps1
Step 4: Verify the active Python is from the venv.
# macOS/Linux
which python
# Windows PowerShell
Get-Command python
Tip: For new Python 3 projects, the built‑in venv
module (Method 1) is preferred, but virtualenv
remains useful in some setups.
Optional — Create a Conda environment (if you use Anaconda/Miniforge)
Step 1: Create an isolated Conda environment with a chosen Python version.
conda create --name myenv python=3.11
Step 2: Activate the environment.
conda activate myenv
Step 3: Install packages with Conda or pip as needed.
conda install numpy
# or
python -m pip install requests
Step 4: Select the Conda interpreter in editors like VS Code.
Why this improves your workflow
Before using virtual environments, package installs land in the global Python and clash across projects. After adopting venv
, each project gets its own interpreter and site‑packages directory, which prevents version conflicts, simplifies upgrades, and makes builds reproducible via requirements.txt
.
That’s it—create the venv, activate, install your packages, and commit your code without the environment. You’ll spend less time debugging version conflicts and more time shipping features.
Member discussion