Part 6 of 6 · Chapter 1 of 4

Standing on Other People's Code

Nobody writes date parsing or HTTP requests from scratch, because somebody has already written it better and tested it more. Install a real package, import it, and use in three lines what would take an afternoon to build.

Advanced9 min read

Nobody writes date parsing or HTTP requests from scratch, because somebody has already written it better and tested it more. Install a real package, import it, and use in three lines what would take an afternoon to build.

pip installs code, not magic

Terminal
$ $ pip install requests$ Successfully installed requests-2.31.0

pip downloads the requests package from the Python Package Index and copies its files onto your machine — nothing more mysterious than that. Once installed, import requests works exactly like importing any module you wrote yourself.

Terminal
$ >>> import requests$ >>> response = requests.get("https://api.github.com")$ >>> response.status_code$ 200

A virtual environment is a clean room per project

Installing packages globally means every project on the machine shares one set of versions — upgrade requests for one project, and a different project that needed the older version breaks without a single line of its own code changing.

Terminal
$ $ python -m venv .venv$ $ source .venv/bin/activate$ (.venv) $ pip install requests

python -m venv .venv creates an isolated folder with its own copy of Python and its own package list. source .venv/bin/activate switches the current terminal to use that copy, so anything installed afterward stays scoped to this one project.

Reading documentation instead of guessing at an API

response.status_code works because someone who wrote requestschose that name and documented it — there is no way to discover it by reading Python's own syntax rules, only by reading what the package actually promises.

Key takeaways

  • pip install <package> downloads code from the Python Package Index and makes it importable, same as any module you wrote.
  • A virtual environment gives each project its own isolated set of installed packages, so upgrading one project never breaks another.
  • python -m venv .venv creates it; source .venv/bin/activate switches the current terminal to use it.
  • A package's own API — its function and argument names — can only be learned from its documentation or docstrings, not guessed from Python's syntax.