Virtual Environments and pip
Create isolated environments with venv, install packages, manage requirements.txt.
Virtual Environments and pip is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Virtual Environments?
A virtual environment is an isolated Python installation for a single project. Each project gets its own set of packages, so upgrading NumPy for one project can never break another.
Without isolation, all packages install globally and version conflicts become unavoidable across data science projects.
Creating a venv
The built-in venv module creates an environment in a folder you name (commonly .venv).
Run this inside your project root. It creates a self-contained copy of the interpreter and a place for packages.
python -m venv .venvActivating on macOS / Linux
Activation puts the environment's python and pip at the front of your PATH. Your prompt usually shows (.venv) when active.
source .venv/bin/activateActivating on Windows
On Windows the activation script lives under Scripts. PowerShell and cmd use slightly different commands.
# PowerShell
.venv\Scripts\Activate.ps1
# cmd.exe
.venv\Scripts\activate.batDeactivating
When you are done, deactivate restores your normal shell PATH. The environment files remain on disk for next time.
deactivateInstalling Packages with pip
With the environment active, pip install places packages inside .venv only. Install the data science core in one line.
pip install numpy pandas matplotlib scikit-learnPinning a Specific Version
Use == to lock an exact version, which keeps results reproducible. Use >= for a minimum.
pip install numpy==1.26.4
pip install "pandas>=2.0"Freezing Dependencies
pip freeze prints every installed package with its exact version. Redirect it into requirements.txt to capture the full environment.
pip freeze > requirements.txtRestoring from requirements.txt
A teammate (or your CI server) recreates the exact environment with -r. This is the heart of reproducible data science.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtWhat requirements.txt Looks Like
It is a plain text file, one pinned package per line. You can edit it by hand to relax or tighten versions.
numpy==1.26.4
pandas==2.2.2
matplotlib==3.8.4
scikit-learn==1.4.2Ignoring venv in Git
Never commit the environment folder; it is large and machine-specific. Commit requirements.txt instead and ignore the folder in .gitignore.
# .gitignore
.venv/
venv/
__pycache__/
*.pycQuick Check
Test your understanding of dependency management.
Recap
You learned the reproducibility workflow:
python -m venv .venvcreates an isolated environmentsource .venv/bin/activate/deactivatetoggle itpip installadds packages locallypip freeze > requirements.txtcaptures versionspip install -r requirements.txtrestores them- Add
.venv/to.gitignore
Frequently asked questions
Is the “Virtual Environments and pip” lesson free?
Yes — the full text of “Virtual Environments and pip” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Virtual Environments and pip”?
Create isolated environments with venv, install packages, manage requirements.txt. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Virtual Environments and pip” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Virtual Environments and pip
- Jupyter Notebooks for Data Science
- Python Data Types for Data Science
- Working with the Python REPL and IPython