Reading a requirements.txt
A tiny reader that mirrors what pip does with each line: skip comments and blanks, split a name from its rule, and label how strict the rule is. The real parser is far more complete.
A virtual environment is a private folder of installed packages for one project, so projects with different needs do not fight. pip installs packages from PyPI into it, and a requirements.txt file records what to install again.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write the five terminal commands for macOS or Linux, one per line and in order: create a venv named .venv, activate it, install the package rich, record the installed packages in requirements.txt, and deactivate.
Write a function is_pinned(line) that takes one requirements line such as "requests==2.32.3" and returns True only when the line contains ==. Print the result for "requests==2.32.3" and for "numpy>=1.26".
Write a program that prints inside venv when sys.prefix is different from sys.base_prefix, and outside venv when they are equal.
Explain it without notes
Why do we use virtual environments?
What happens when you activate a venv, and what does python -m pip guarantee?
What is requirements.txt, and what is the difference between ==, >= and no version?
What does the error externally-managed-environment mean, and what should you do?
Expected output
requests ==2.32.3 pinned exactly
numpy >=1.26,<3 a range
pandas (any version) unpinned