Chapter 4: Django – Create Virtual Environment
Today we’re zooming in on one single, super-important topic: Creating a Virtual Environment for Django.
This is Step Zero in every real Django project (even in January 2026). Skip it, and you’ll regret it later when versions clash or your friend’s laptop blows up trying to run your code.
I’ll explain why, what options exist in 2026, what most people actually use, and then walk you through the recommended beginner-friendly way with every command, screenshot-in-words, common mistakes (especially on Windows), and pro tips.
Why Do We Even Need a Virtual Environment? (The Real Talk)
Think of your computer’s Python as a shared kitchen in a PG:
- Everyone throws in their masalas (libraries like Django 6.0.1, requests 2.32, etc.)
- One person upgrades masala X to fix their biryani → suddenly everyone’s idli batter tastes weird
- Chaos!
A virtual environment = your private kitchen inside the PG:
- Own copy of Python interpreter
- Own pip + installed packages
- No effect on global/system Python or other projects
- Easy to delete / recreate / share (just send requirements.txt)
For Django specifically:
- Different projects need different Django versions (e.g., old client on 4.2, new one on 6.0)
- Avoid polluting your system Python (many tools like VS Code extensions expect clean global install)
- Makes deployment 100× easier (Docker, Railway, Render all love isolated envs)
Official Django docs + community in 2026 still say: Use virtual environments. Always.
What Tools Exist in 2026? (Quick Comparison Table)
| Tool | Built-in? | Ease for Beginners | Speed | Modern Features (lock file, etc.) | Recommendation in 2026 for Django beginners |
|---|---|---|---|---|---|
| venv | Yes (Python 3.3+) | ★★★★★ Very easy | Fast | Basic (no lock file auto) | Best starting point — what I’ll teach you today |
| virtualenv | No (pip install) | ★★★★☆ | Faster than venv sometimes | Basic | Still good, but venv is enough now |
| pipenv | No | ★★★☆☆ | Medium | Pipfile + lock + auto venv | Okay, but slower & sometimes buggy |
| Poetry | No | ★★★☆☆ | Medium-Fast | pyproject.toml + lock + groups | Great for medium+ projects, steeper curve |
| uv (new hotness) | No | ★★★★☆ | Blazing fast | Replaces pyenv + venv + pip | Rising star — try later, not first project |
Verdict for you right now (Jan 31, 2026, learning phase): Use built-in venv — zero extra installs, works everywhere, taught in official Django tutorial, zero surprises.
(Once comfortable → try Poetry or uv for bigger projects.)
Step-by-Step: Creating & Using venv Like a Pro
1. Open Terminal / PowerShell / CMD
- Windows: Search “PowerShell” (better than old CMD) or “cmd”
- macOS: Terminal
- Linux: Terminal
Navigate to where you want your project (never Desktop/Downloads):
|
0 1 2 3 4 5 6 |
mkdir django-learn-2026 && cd django-learn-2026 |
(Use any name you like — myproject, webliance-polls, etc.)
2. Create the virtual environment
Name it venv (most common) or .venv (hidden, popular in 2026).
I recommend venv for beginners — easier to see.
|
0 1 2 3 4 5 6 7 |
# This creates a folder called "venv" with its own Python python -m venv venv |
Or (if your system uses python3 alias):
|
0 1 2 3 4 5 6 |
python3 -m venv venv |
What happens inside venv/ folder?
- bin/ or Scripts/ → python, pip, activate scripts
- lib/ → where packages get installed
- pyvenv.cfg → config file
Takes 2–10 seconds.
Common mistake: Running without -m → venv venv (wrong!)
3. Activate it (super important!)
This changes your shell to use the venv’s Python & pip.
-
Windows (PowerShell — recommended):
PowerShell0123456.\venv\Scripts\Activate.ps1(If blocked: run Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned once)
-
Windows (CMD):
cmd0123456venv\Scripts\activate.bat -
macOS / Linux / Git Bash:
Bash0123456source venv/bin/activate
Success looks like:
|
0 1 2 3 4 5 6 7 8 9 |
(venv) C:\Users\Webliance\django-learn-2026> # or (webliance@hyderabad:~/django-learn-2026) source venv/bin/activate (venv) webliance@hyderabad:~/django-learn-2026$ |
You now have isolated Python!
Check:
|
0 1 2 3 4 5 6 7 8 9 |
which python # macOS/Linux → should show .../venv/bin/python where python # Windows python --version # should match your system Python pip list # almost empty — only pip & setuptools |
4. Install Django inside the venv (now safe!)
|
0 1 2 3 4 5 6 7 |
pip install Django==6.0.1 # or just pip install django (gets latest 6.0.x) |
Now pip list shows Django!
5. Test it — create first project
|
0 1 2 3 4 5 6 |
django-admin startproject mysite . |
( dot = current folder)
Then:
|
0 1 2 3 4 5 6 |
python manage.py runserver |
→ http://127.0.0.1:8000/ → rocket page!
All packages are inside venv/ — your global Python is untouched.
6. Deactivate when done
Just type:
|
0 1 2 3 4 5 6 |
deactivate |
Prompt goes back to normal. You can close terminal — next time cd back and activate again.
Pro Tips & Hyderabad Survival Guide (2026 edition)
-
VS Code integration — after activation, VS Code auto-detects venv. Or press Ctrl+Shift+P → “Python: Select Interpreter” → choose ./venv/bin/python or ./venv/Scripts/python.exe
-
requirements.txt — after installing packages:
Bash0123456pip freeze > requirements.txtShare this file — friend does pip install -r requirements.txt
-
Power cuts / laptop sleep — just reactivate when back
-
gitignore — add venv/ to .gitignore (never commit virtual env)
-
Multiple projects — each folder gets its own venv/ — no conflict
-
If you mess up — delete venv/ folder and recreate — takes seconds
Quick Recap Commands (Copy-Paste Friendly)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
# New project mkdir my-django && cd my-django python -m venv venv # Windows PowerShell → .\venv\Scripts\Activate.ps1 # macOS/Linux → source venv/bin/activate pip install Django==6.0.1 django-admin startproject mysite . python manage.py runserver |
Done!
Now tell me, boss:
- Did it work? Any error? Paste it — we’ll fix in 2 min
- Want to continue to “First project + runserver deep dive”?
- Or “How to upgrade packages safely” / “requirements vs poetry later”?
- Or show me your folder structure screenshot (describe it)?
I’m right here — let’s make sure your setup is rock-solid before we build cool stuff! 🚀🇮🇳
