Summary - Packaging
- Python course
- Advanced
- Lesson 78/83
- 5 min
Combine pyproject.toml, venv and pip to write a packageable project.
Summary - Packaging
In this group you learned the core tools of Python packaging:
- pyproject.toml: project identity and dependencies.
- setuptools: the build tool via
[build-system]. - venv: an isolated virtual environment.
- pip install -e .: editable install,
sys.path. - requirements.txt: a frozen dependency list.
In the final task you will use all of them together: read pyproject content with tomllib and print the dependency count.
Practice Print the project name and the dependency count
Start with import tomllib. Convert the TOML string below into structured data with data = tomllib.loads(...):
[project]
name = "demo"
version = "0.1.0"
dependencies = ["requests>=2.28", "httpx==0.24"]Then print the project name with print(data["project"]["name"]) and the dependency count with print(len(data["project"]["dependencies"])).
Starter code
# bring in the tomllib module
# load the TOML string as structured data
# print the project name
# print the dependency countSample solution
import tomllib
data = tomllib.loads('''
[project]
name = "demo"
version = "0.1.0"
dependencies = ["requests>=2.28", "httpx==0.24"]
''')
print(data["project"]["name"])
print(len(data["project"]["dependencies"]))Expected output
demo 2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the packaging tools
| Term | Meaning |
|---|---|
pyproject.toml |
Project identity |
setuptools |
Build tool |
venv |
Virtual environment |
requirements.txt |
Dependency list |
2 Which of the following is a valid table in pyproject.toml?
- [python]
- [project] Correct answer
- [import]
- [package]
Explanation: The [project] table holds the package identity.