Python Environments and Dependencies
Virtual Environments and pyproject.toml
BOTIE
Builds had another dependency problem: a tool could need packages before it could execute the script that described them. PEP 518 introduced pyproject.toml build-system requirements in 2016 so tools could read that setup without first running its build script. Package isolation and declarative build requirements address different parts of reproducibility.
Sources for this history
The Same Source Imports Different Packages
Botie's report works on one computer and fails on another. Both copies have the same source, but they use different package versions. A global installation intended for another project changed the setup.
We need to describe the project's requirements and separate its installed packages. Reproducing a run also depends on the Python version and the resolved indirect dependencies.
This compares supplied version labels exactly. It does not install packages, resolve version ranges, or establish that matching versions work on the target machine.
Step 1 of 1
Each project can keep its own package set
A shared source file can behave differently when its imports resolve to different installations.
| Project | Environment | Recorded package |
|---|---|---|
| Report A | A's .venv | parser 2.1 |
| Report B | B's .venv | parser 3.0 |
Report A
- Environment
- A's .venv
- Recorded package
- parser 2.1
Report B
- Environment
- B's .venv
- Recorded package
- parser 3.0
Choose the interpreter and environment
Checking each package catches a missing installation as well as a changed version. The returned row shows what was expected and what was found.
Check the report environment
The report passed in the reviewed environment but behaves differently on another machine. Compare the installed packages with the saved version list. Each row keeps the package name and both version labels together; the package names here are invented.
BOTIE
Package rows with different reviewed and installed labels
- package
- sensor-reader
- reviewed
- "2.1"
- installed
- "2.2"
| package | reviewed | installed |
|---|---|---|
| sensor-reader | "2.1" | "2.2" |
Follow the operations
1. Reviewed and installed package versions
- package
- sensor-reader
- reviewed
- "2.1"
- installed
- "2.2"
- package
- report-writer
- reviewed
- "1.0"
- installed
- "1.0"
package reviewed installed sensor-reader "2.1" "2.2" report-writer "1.0" "1.0" 2. Read a field from every record
- 0"2.2"
- 1"1.0"
3. Read a field from every record
- 0"2.1"
- 1"1.0"
4. Compare actual with expected
- 0true
- 1false
5. Keep flagged entries
- package
- sensor-reader
- reviewed
- "2.1"
- installed
- "2.2"
package reviewed installed sensor-reader "2.1" "2.2" 6. Result
- package
- sensor-reader
- reviewed
- "2.1"
- installed
- "2.2"
package reviewed installed sensor-reader "2.1" "2.2"
This compares supplied version labels exactly. It does not install packages, resolve version ranges, or establish that matching versions work on the target machine.
1. Choose the interpreter and environment
A virtual environment has its own interpreter entry point and installation directory for packages. It prevents an ordinary package installation for one project from automatically replacing another project's packages. It commonly shares the base interpreter's standard library.
The environment is not a security sandbox. Installed code runs with the process's permissions. Nor does creating an environment automatically select the right Python version. Start with the interpreter supported by the project.
2. Keep installation commands tied to that interpreter
python -m pip runs pip through the named interpreter. Using the environment's interpreter explicitly helps avoid installing a package with one Python and running the program with another.
In your own project, create the environment with python -m venv .venv. On Windows its interpreter is .venv/Scripts/python.exe; on macOS or Linux it is .venv/bin/python. These are local commands, not steps performed inside the browser. Keep the environment directory out of version control and retain the files that recreate its setup.
Check yourself
Questions on Parts 1 and 2
Question 1 of 2
What does a virtual environment help separate?
Question data
Project A and project B require different package versions.3. Separate declarations from a resolved installation
A project can declare direct runtime requirements in pyproject.toml. Build-system requirements identify tools needed to build it, which differs from packages needed at runtime. Development tools also need a documented installation workflow.
A compatible range states supported choices. An exact pin records one choice. A lock workflow resolves direct and indirect dependencies and records the information its tool uses to recreate that resolution. A few direct pins do not necessarily preserve transitive versions, distribution hashes or platform-specific choices.
Read metadata without executing setup code
tomllib reads a configuration string into ordinary dictionaries and lists.
import tomllib
text = '''
[project]
name = "greenhouse-report"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["example-parser>=2,<3"]
'''
metadata = tomllib.loads(text)
print(metadata["project"]["name"])
print(metadata["project"]["dependencies"])greenhouse-report
['example-parser>=2,<3']This invented dependency is metadata for inspection. Reading the requirement string does not install or resolve it.
4. Check the recreated environment
A snapshot comparison can reveal drift from a reviewed installation. It cannot establish that all packages behave correctly or that suitable distributions exist on a different platform. Recreate the setup, run imports on the intended platform, then run the relevant tests.
Update dependencies deliberately. Review compatibility and the resolved changes instead of fetching an unreviewed newest package on every run. Keep interpreter, platform and package-source assumptions with the project.
Compare an exact version snapshot
Extra packages do not change whether the requested versions match.
required = {"parser": "2.1", "writer": "1.4"}
installed = {"parser": "2.2", "writer": "1.4", "extra": "9"}
different = sorted(name for name, version in required.items()
if installed.get(name) != version)
print(different)['parser']This is text equality for a snapshot, not semantic-version compatibility resolution.
Check yourself
Questions on Parts 1 to 4
Question 1 of 4
Which describes the application's runtime requirements?
Question data
pyproject.toml contains build-system.requires and project.dependencies.Try it with Botie
Build with Botie
Write and run Python