Your college project uses pandas 2.2. A tutorial you're following needs an older version of the same library. Install one and the other project breaks. Worse, a package you installed months ago for something else clashes with both. This mess has a standard fix: give each project its own private set of packages. That's what pip and virtual environments in Python are for.
pip downloads and installs packages from the Python Package Index (PyPI), which hosts over 6 lakh projects. A virtual environment (venv) is a folder that holds its own copy of Python's package list, so each project's packages stay separate. Every professional Python project uses both, and setting them up takes about two minutes.
What you need first: how to install Python and Python modules and packages.
Key terms
- Package: code someone else wrote and published, such as pandas or requests.
- PyPI: the Python Package Index at pypi.org, the public website pip downloads packages from.
- pip: the tool that installs, upgrades and removes packages. It comes with Python.
- Dependency: a package your project needs. Packages often have their own dependencies, which pip installs too.
- Virtual environment (venv): a folder with its own Python and its own installed packages, separate from every other project.
- requirements.txt: a text file listing a project's packages and versions, so others can install the same set.
Running pip the safe way
You'll see pip install pandas in most tutorials. It works, but if you have more than one Python installed, plain pip may belong to a different Python from the one that runs your code. Then you install a package, run your script and still get ModuleNotFoundError.
The safe habit is to run pip through the Python you're using:
python -m pip install requests
-m pip means "run the pip module of this exact Python". On Mac and Linux, use python3 instead of python if that's how you start Python. On Windows, py -m pip also works.
Basic pip commands
| Task | Command |
|---|---|
| Install a package | python -m pip install requests |
| Install a specific version | python -m pip install "pandas==2.2.3" |
| Install a minimum version | python -m pip install "pandas>=2.0" |
| Upgrade a package | python -m pip install --upgrade pandas |
| Remove a package | python -m pip uninstall pandas |
| List installed packages | python -m pip list |
| Show details of one package | python -m pip show pandas |
| Save the exact versions | python -m pip freeze > requirements.txt |
| Install from a list | python -m pip install -r requirements.txt |
| Upgrade pip itself | python -m pip install --upgrade pip |
Put version specifiers in quotes. Without them, > in pandas>=2.0 is read by the terminal as "save output to a file".
The package name you install isn't always the name you import. You install scikit-learn but write import sklearn; you install beautifulsoup4 but write import bs4. The package's PyPI page tells you both.
Why you need a virtual environment
Without a virtual environment, every package goes into one shared place for that Python installation. Problems that follow:
- Version clashes: project A needs Django 4, project B needs Django 5. Only one can be installed.
- "Works on my machine": your code depends on a package you installed long ago and forgot about, so it fails on your teammate's laptop.
- Messing up system Python: on Linux and Mac, the operating system uses its own Python. Installing into it can break system tools, which is why newer systems refuse with
error: externally-managed-environment.
A virtual environment fixes all three. Each project folder gets its own .venv folder, with its own packages.
How to create and activate a virtual environment
Python has a built-in module for this called venv. Open a terminal in your project folder and run:
python -m venv .venv
This creates a folder called .venv holding a private Python and an empty package list. The name .venv is a common convention; VS Code finds it automatically.
Next, activate it, which makes python and pip in this terminal point to the environment:
| System | Command |
|---|---|
| Windows (Command Prompt) | .venv\Scripts\activate |
| Windows (PowerShell) | .venv\Scripts\Activate.ps1 |
| Mac or Linux | source .venv/bin/activate |
When it's active, your prompt starts with (.venv). Now install what the project needs:
python -m pip install pandas matplotlib
These packages go into .venv only. To leave the environment, run:
deactivate
You need to activate again each time you open a new terminal. VS Code can do it for you: press Ctrl+Shift+P, choose "Python: Select Interpreter", and pick the one inside .venv. New terminals in VS Code then activate it automatically.
Checking which Python you're using
If you're ever unsure whether the environment is active, ask Python:
import sys
print(sys.executable)
print(sys.prefix != sys.base_prefix)
The first line shows the path of the Python that's running. Inside a virtual environment it ends in something like .venv\Scripts\python.exe on Windows or .venv/bin/python on Mac and Linux. The second line prints True inside a virtual environment and False outside one.
The PowerShell error on Windows
On Windows, PowerShell often blocks the activation script with an error saying that running scripts is disabled on this system. Allow scripts you've created for your own user account with:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Then try activating again. If you'd rather not change the setting, use Command Prompt and .venv\Scripts\activate instead.
Sharing a project with requirements.txt
Never send or upload the .venv folder. It's big, and it only works on the computer that made it. Instead, save the list of packages:
python -m pip freeze > requirements.txt
requirements.txt then contains lines like pandas==2.2.3, one for each installed package and its exact version. Add it to your project and to Git.
Someone else (or you, on a new laptop) recreates the environment with:
python -m venv .venv
.venv\Scripts\activate
python -m pip install -r requirements.txt
Use source .venv/bin/activate for the second line on Mac or Linux.
Add .venv/ to your .gitignore file so Git never uploads it.
pip freeze lists everything, including packages that were pulled in as dependencies. Many developers prefer to write requirements.txt by hand with just the packages they chose, like pandas>=2.2, and let pip work out the rest. Both approaches are fine for learning projects.
A complete workflow
Starting a new project from nothing:
mkdir expense-tracker
cd expense-tracker
python -m venv .venv
.venv\Scripts\activate
python -m pip install --upgrade pip
python -m pip install pandas matplotlib
python -m pip freeze > requirements.txt
Then write your code in this folder, with the .venv interpreter selected in VS Code. Every time you come back: open the folder, activate, work, deactivate when you're done.
Other tools you'll hear about
- conda (Anaconda and Miniconda): popular in data science, because it also installs non-Python libraries. Many universities use it. It has its own environment commands, so don't mix it with
venvin the same project. - uv: a newer, very fast tool from Astral that replaces pip and venv with commands such as
uv venvanduv pip install. It's become popular since 2024. - Poetry and pipenv: tools that manage dependencies and environments together for larger projects.
Our advice: learn plain pip and venv first. They ship with Python, every tutorial assumes them, and the other tools build on the same ideas.
Where this is used
- Every job that uses Python expects you to set up a project's environment from its
requirements.txt(orpyproject.toml) on day one. - Deployment: servers and Docker images install exactly what's listed, so a missing line in
requirements.txtbreaks the live site. - Data science: pinning versions means your analysis gives the same results next month.
Common mistakes
- Installing with
pipand running with a differentpython, which givesModuleNotFoundErrorfor a package you "just installed". Usepython -m pip. - Forgetting to activate the environment in a new terminal.
- Uploading
.venvto GitHub. Add it to.gitignore. - Using
sudo pip installon Mac or Linux, which changes the system's Python. Use a virtual environment. - Copying a
.venvfolder to another computer or folder. Recreate it fromrequirements.txtinstead.
Practice
- Write the commands to create a virtual environment called
.venv, activate it on Windows Command Prompt, and installrequests.
Show the answer
python -m venv .venv
.venv\Scripts\activate
python -m pip install requests
- You run
pip install pandas, which succeeds, butpython analysis.pystill saysModuleNotFoundError: No module named 'pandas'. Give two likely causes.
Show the answer
First, pip and python belong to different Python installations, so pandas went into the other one. Fix it by installing with python -m pip install pandas. Second, the virtual environment isn't active in this terminal, or VS Code is using a different interpreter. Activate it, or pick the .venv interpreter.
- What file should you share so a teammate can install the same packages, and what command do they run?
Show the answer
Share requirements.txt (made with python -m pip freeze > requirements.txt). They create and activate their own virtual environment, then run python -m pip install -r requirements.txt.
- You want exactly version 1.26.4 of NumPy. What do you type?
Show the answer
python -m pip install "numpy==1.26.4"
The quotes stop the terminal from treating special characters such as > or < as redirections, which matters for >=; they're harmless with ==.
- Why shouldn't the
.venvfolder go into Git?
Show the answer
It's large, it contains files specific to your operating system and Python version, and it can be rebuilt from requirements.txt in a minute. Add .venv/ to .gitignore.
Interview questions
What is a virtual environment and why use one?
It's an isolated directory with its own Python interpreter link and its own installed packages. It keeps each project's dependencies separate, avoids version conflicts between projects, protects the system Python, and makes setups reproducible with a requirements file.
What is the difference between pip install and python -m pip install?
python -m pip runs pip as a module of that specific Python interpreter, so packages are guaranteed to go into the same Python you run your code with. Bare pip runs whichever pip executable is first on your PATH, which may belong to another installation.
What does pip freeze do?
It prints every installed package in the current environment with its exact version, in name==version format. Redirected to requirements.txt, it records the environment so it can be reproduced with pip install -r.
Key takeaways
- pip installs packages from PyPI; run it as
python -m pipso it matches your Python. - Create one virtual environment per project with
python -m venv .venv, and activate it before installing. - Activation differs:
.venv\Scripts\activateon Windows,source .venv/bin/activateon Mac and Linux. - Share
requirements.txt, never the.venvfolder. - If a package is "installed" but can't be imported, check which Python and which environment you're using.
Frequently asked questions
Do I need a virtual environment for every project?
For anything you'll keep, share or come back to, yes. For a quick one-off script that only uses the standard library, you can skip it.
Is venv the same as virtualenv?
They do the same job. venv is built into Python 3.3 and later; virtualenv is an older third-party tool with a few extra features. Beginners only need venv.
Where does pip install packages?
Into the site-packages folder of the Python that runs it. Inside an active virtual environment, that's inside the .venv folder. python -m pip show pandas prints the location.
What to learn next
- Handle problems in your programs with Python exceptions: try, except and raise.
- Revise how imports find packages in Python modules and packages.
- Use what you've installed in the NumPy array tutorial.