Sub topic 3
Pin down the versions you are actually running
This chapter assumes you can run Python from a terminal, use pip and virtual environments, and read a requirements.txt file. Every check later in this tutorial depends on one fact: which version of each library your code runs against. Without it, you cannot tell whether an AI suggestion is wrong or just written for a different release.
Why the version decides the answer
A call such as DataFrame.append is correct in pandas 1.x and an error in pandas 2.0 and later. The assistant's suggestion has no version attached, so you must supply the version yourself. Check the Python version too. Some standard library calls, such as datetime.utcnow(), are deprecated from Python 3.12.
There are three versions to know:
- The Python interpreter version.
- The installed version of each library, which may differ from what a file says.
- The version your project declares it needs.
When these disagree, the installed version is what your code runs against.
Read the installed versions
Use importlib.metadata from the standard library. It reads the metadata of the interpreter that runs the script, so the answer matches your runtime.
import sys
from importlib.metadata import PackageNotFoundError, version
def report(*names):
print("python", sys.version.split()[0])
for name in names:
try:
print(name, version(name))
except PackageNotFoundError:
print(name, "NOT INSTALLED")
report("requests", "pandas", "numpy")The function takes the distribution name, which is the name you give to pip install. It is not always the import name. You import PIL, but the distribution is Pillow. You import sklearn, but the distribution is scikit-learn.
From the shell, call pip through the interpreter so you query the same environment your code uses:
python -m pip list
python -m pip show requests
python -m pip freeze > requirements.txtA bare pip command may belong to a different Python than the one that runs your tests. This mismatch is a common cause of confusing results.
| Question | Command or source |
|---|---|
| Which interpreter is running? | `sys.version` or `python --version` |
| Which version of one library is installed? | `importlib.metadata.version("name")` |
| What is installed overall? | `python -m pip list` |
| What did the project ask for? | `pyproject.toml`, `requirements.txt` |
Pin what you depend on
A declaration such as requests>=2 accepts many releases, so two machines can install different code. Pinning means fixing an exact version:
requests==2.32.3
pandas==2.2.3Pins make the question "which version?" have one answer for everyone. They also make the AI prompt and the review concrete: you can say "pandas 2.2.3" instead of "a recent pandas". For larger projects, generate a full lock file with a tool such as pip-tools or uv, so transitive dependencies are pinned too. Transitive dependencies are the libraries your libraries need.
Record the versions with the question
When you ask an assistant for code, or when you review its output, write the versions down first. A short header is enough:
Python 3.12.4
requests 2.32.3
pandas 2.2.3Later chapters use this header in prompts and in the review checklist. For now, treat it as the first step of every check: find the versions, then verify the code against them.
Common errors
- Querying the wrong environment. The virtual environment is not activated, so
pipandpythonpoint to different installations. - Using the import name instead of the distribution name with
version(). - Trusting the version in documentation you happen to have open. Documentation sites often default to the latest release, which may be newer than yours.
- Reading the declared range as the installed version.
Exercise
You run version("sklearn") and get PackageNotFoundError, although import sklearn works. What is wrong, and what call fixes it?
Show answer
version() takes the distribution name, which is the name used with pip install, not the import name. The distribution is scikit-learn, so version("scikit-learn") returns the installed version.
Exercise
Your requirements.txt says pandas>=2.0, but an assistant's code using DataFrame.append works on your colleague's machine and fails on yours. Which two checks tell you why?
Show answer
Run python -m pip show pandas (or importlib.metadata.version("pandas")) in each environment to compare the installed versions. The colleague likely has pandas 1.x, where append still exists, despite the declared range. Then pin an exact version so both machines match.