pyproject-parser
pyproject-parser is a Python library designed to parse and access data from 'pyproject.toml' files, adhering to PEP 517 and PEP 621 specifications. It simplifies programmatic access to project metadata, build system configurations, and other sections defined in 'pyproject.toml'. The library is actively maintained, with frequent releases, often including pre-releases (betas) before stable versions, and is currently at version 0.14.0.
Common errors
-
AttributeError: 'PyProject' object has no attribute 'project_name'
cause In versions 0.10.0 and later, the `project_name` property was removed and replaced by the `name` property to align with PEP 621.fixChange `project.project_name` to `project.name`. -
TypeError: PyProject.from_path() got an unexpected keyword argument 'path'
cause The argument for specifying the TOML file path was renamed from `path` to `filename` in version 0.10.0.fixUpdate the call to use `PyProject.from_path(filename=my_path)` instead of `PyProject.from_path(path=my_path)`. -
ImportError: cannot import name 'parse' from 'pyproject_parser'
cause The standalone `parse` and `load` helper functions were removed in version 0.12.0, consolidating parsing functionality into the `PyProject` class.fixRefactor code to use `PyProject.from_path()` for file parsing or `PyProject.from_string()` for string content.
Warnings
- breaking Major API overhaul in versions 0.9.0 and 0.10.0 introduced significant breaking changes to the PyProject class's constructor and property names (e.g., `project_name` was renamed to `name`, `project` property removed).
- breaking The `PyProject.from_path()` method's `path` argument was renamed to `filename` in version 0.10.0.
- breaking The top-level `parse` and `load` helper functions (e.g., `from pyproject_parser import parse`) were removed in version 0.12.0.
Install
-
pip install pyproject-parser
Imports
- PyProject
from pyproject_parser.parser import PyProject
from pyproject_parser import PyProject
Quickstart
from pyproject_parser import PyProject
from pathlib import Path
# Create a dummy pyproject.toml for demonstration
dummy_toml_content = '''
[project]
name = "my-awesome-project"
version = "0.1.0"
authors = [
{name = "John Doe", email = "john@example.com"}
]
description = "A minimal project"
readme = "README.md"
requires-python = ">=3.8"
keywords = ["python", "example"]
classifiers = [
"Programming Language :: Python :: 3"
]
[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"
'''
dummy_toml_path = Path("dummy_pyproject.toml")
dummy_toml_path.write_text(dummy_toml_content)
try:
project = PyProject.from_path(dummy_toml_path)
print(f"Project Name: {project.name}")
print(f"Project Version: {project.version}")
print(f"Required Python: {project.requires_python}")
if project.authors:
print(f"Author: {project.authors[0].name} <{project.authors[0].email}>")
finally:
# Clean up the dummy file
dummy_toml_path.unlink()