PY-101 ยท Modules and packages
pip and virtual environments
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
pip installs packages from PyPI, such as pip install requests. A virtual environment, made with python -m venv .venv, is a private folder of packages for one project, so projects never fight over versions. requirements.txt lists a project's packages so anyone can recreate it with pip install -r requirements.txt.
- Install packages with pip
- Create and activate a venv
- Use requirements.txt
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
requirements = """requests==2.32.3pandas>=2.2# comment lines are ignoredpytest"""for line in requirements.splitlines():line = line.strip()if not line or line.startswith("#"):continuename = line.replace(">=", "==").split("==")[0]print(name)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
requests
pandas
pytest ๐ฏ From the requirements text, print only the packages that are pinned to an exact version (contain ==), as a list: ['requests', 'numpy']
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
[line.split("==")[0] for line in text.splitlines() if "==" in line]
- The problemProject A needs pandas 1.5 and project B needs pandas 2.2. Installed globally, one of them breaks. A venv per project keeps both happy.
- The Python idea
python -m venv .venvpip installs packages from PyPI, such as pip install requests. - In real lifeCloud deployments (Azure App Service, Docker images) read requirements.txt to install exactly the packages your app needs.
Start with a question
Project A needs pandas 1.5 and project B needs pandas 2.2. Installed globally, one of them breaks. A venv per project keeps both happy.
Every professional Python project uses an isolated environment and a pinned list of dependencies.
Explain it like I'm 10
A virtual environment is a separate pencil case for each subject, so the maths pencils never get mixed up with the art ones.
The technical explanation
Create: python -m venv .venv. Activate: source .venv/bin/activate (macOS/Linux) or .venv\Scripts\activate (Windows).
Install: pip install requests==2.32.3. Save: pip freeze > requirements.txt.
This browser editor cannot run pip, but Python Academy's playground can import numpy, pandas and matplotlib directly.
Syntax
python -m venv .venv
source .venv/bin/activate
pip install requests
pip install -r requirements.txt Real-world scenario
Cloud deployments (Azure App Service, Docker images) read requirements.txt to install exactly the packages your app needs.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: if __name__ == "__main__" โQuestions people ask
What is the difference between pip and conda?
pip installs Python packages from PyPI. conda is a separate package and environment manager popular in data science that can also install non-Python libraries.
Do I need to activate the virtual environment every time?
Yes, in each new terminal. Editors like VS Code can select the venv for you automatically.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the difference between pip and conda?
pip installs Python packages from PyPI. conda is a separate package and environment manager popular in data science that can also install non-Python libraries.
beginner Do I need to activate the virtual environment every time?
Yes, in each new terminal. Editors like VS Code can select the venv for you automatically.
intermediate What does pip do?
Installs packages. pip installs from PyPI.
intermediate Why use a virtual environment?
Separate packages per project. Projects stop clashing over versions.
Quick revision
Remember
- Install packages with pip
- Create and activate a venv
- Use requirements.txt
- Add
.venv/to .gitignore. Commit requirements.txt, never the venv folder itself.
Syntax
python -m venv .venv
source .venv/bin/activate
pip install requests
pip install -r requirements.txt Official documentation
- Virtual environments and packages (docs.python.org)