Skip to main content

project-paths

Lint and Test codecov PyPI

Access file paths from pyproject.toml

Thanks to @Madoshakalaka for the idea!

# pyproject.toml
[tool.project-paths]
readme = "README.md"
# app.py
from project_paths import paths

# paths.readme is a pathlib.Path object:
print(paths.readme.read_text())

Install

pip install project-paths

Usage

Does your application have a bunch of configurable file paths? Do you wish you just had one place to configure list them all?

Add paths to [tool.project-paths]

With this module, define your paths in your pyproject.toml file under the [tool.project-paths] table:

[tool.project-paths]
docs = "path/to/my/docs"
settings = "path/to/my/settings.py"
config = "/opt/path/to/my/config
# Add as many paths as you want!

Anything string defined with [tool.project-paths] will be made available. Relative paths are relative to pyproject.toml.

Access paths using project_paths.paths.<path name>

Now you can access all the paths listed in pyproject.toml with project_paths.paths. Every path is returned as a pathlib.Path object:

from project_paths import paths

print(paths.docs.glob("*.md"))
assert paths.config.exists()
exec(paths.settings.read_text())
# Or anything you want!

Access your project root directory using project_root

You can access the project root directory (assumed to be the directory that contains pyproject.toml) using project_root, which acts like a pathlib.Path object:

from project_paths import project_root

assert project_root.is_dir()
assert (project_root / "pyproject.toml").is_file()
print((project_root / "README.md").read_text())

Note: project_root is not a true Path object. Use Path(project_root) to obtain a true Path object.

Caveats

Names in [tool.project-paths] should be a valid Python identifier and the names cannot have a leading underscore. If a name has a leading underscore, a warning is issued and the name is inaccessible:

[tool.project-paths]
# BAD: paths that start with a '_' cannot be used
_my_path = "path/to/wherever"
# GOOD: path is a valid Python identifier!
my_path = "path/to/wherever"

Errors

All intentional errors inherit from project_paths.ProjectPathError.

PyProjectNotFoundError

Raised when project_paths cannot find an appropriate pyproject.toml for the caller. This can happen if the caller simply does not have a pyproject.toml file, or when it's not possible to determine the caller's filename.

NOTE: if accessing paths from the interactive console, project_paths will find the appropriate pyproject.toml relative to the current working directory!

ConfigurationNotFoundError

Raised when project_paths cannot find the [tool.project-paths] table within the pyproject.toml file. Perhaps there is a typo or perhaps the project_paths inferred the incorrect module path.

License

2021 © Eddie Antonio Santos. MIT Licensed.

Metadata

Release files for project-paths 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for project-paths 1.1.1
File Size Uploaded
project-paths-1.1.1.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for project-paths 1.1.1
File Interpreter ABI Platform
project_paths-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / project-paths-1.1.1.tar.gz

Download URL project-paths-1.1.1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b040f701fcdd550e7fa6ea85fa8a82ae047da755b4d639a1b278da7c959fdada
BLAKE2b-256 checksum
How to use checksums
c70853040f2005ce8dd4229018b4026d3a0de0636d8745f32e67ace1e3a19998
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

Release files / project_paths-1.1.1-py3-none-any.whl

Download URL project_paths-1.1.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e93b759772f47d79f86ad5482c55ef1cc9566de6a4108aab939b6e0e602cf2d
BLAKE2b-256 checksum
How to use checksums
3c23191bf70c5f2cdfedf4e23621ac5d6fd4761ccb74f3cae7ba751b9c3a4eda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page