Modern pyproject.toml Standards (PEP 517 & 518)
Legacy packaging ran arbitrary code inside `setup.py`. Modern Python packaging specifies declarative metadata, build backends, and tool configurations in a single standardized `pyproject.toml` file 📦.
import tomllib
toml_content = """
[build-system]
requires = ["flit_core"]
build-backend = "flit_core.buildapi"
[project]
name = "xicae-core"
version = "1.0.0"
"""
config = tomllib.loads(toml_content)
print(f"Project: {config['project']['name']} v{config['project']['version']} 📦")
# 91. Modern pyproject.toml Standards (PEP 517 & 518) Legacy packaging ran arbitrary code inside `setup.py`. Modern Python packaging specifies declarative metadata, build backends, and tool configurations in a single standardized `pyproject.toml` file 📦. ```python import tomllib toml_content = """ [build-system] requires = ["flit_core"] build-backend = "flit_core.buildapi" [project] name = "xicae-core" version = "1.0.0" """ config = tomllib.loads(toml_content) print(f"Project: {config['project']['name']} v{config['project']['version']} 📦") ``` Python 3.11 added `tomllib` to the standard library as a read-only TOML parser. For generating or modifying TOML files at runtime, write-capable tools use third-party `tomli-w` or `tomlkit` ⚡! --- ### Question Why did modern Python packaging deprecate executable `setup.py` in favor of declarative `pyproject.toml`? 🧠- [ ] a. `setup.py` executed arbitrary executable code just to determine build dependencies, causing security and build isolation failures 📦 - [ ] b. `setup.py` scripts could not be read on macOS or Linux operating systems - [ ] c. TOML is mathematically proven to compile faster than binary C extensions - [ ] d. Pip removed support for any packages written after Python 3.8 --- ### Challenge Use `tomllib.loads` to parse TOML string with `name = 'micro-tool'` and `version = '0.1.0'` under `[project]`. Print `f"{name}=={version}"`. 🎯 <details> <summary>Solution</summary> ```python import tomllib s = """ [project] name = 'micro-tool' version = '0.1.0' """ d = tomllib.loads(s) print(f"{d['project']['name']}=={d['project']['version']}") ``` </details>