pydtl-relativepath tool from pydtl community can be used to solve many issues that occurs with relative paths and imports from various modules in your complex framework.
- Convert Relative Paths to Absolute: Easily convert relative paths to absolute paths based on the current working directory or the current file's directory.
- PathLib Compatibility: Seamlessly works with PathLib, allowing for modern path manipulations.
- Customizable Path Construction: Adjust path construction through parameters like
parent_dir_jump
to navigate through directory structures. - Error Handling: Provides clear error messages for invalid inputs, such as incorrect
relative_type
values orparent_dir_jump
configurations.
To install pydtl-relativepath
, run the following command in your terminal:
pip install pydtl-relativepath
To convert a path relative to the current working directory into an absolute path:
from pydtl_relativepath import rel2abs, RelativePathType
path = rel2abs("README.md", relative_type=RelativePathType.RELATIVE_TO_WORKING_DIRECTORY)
print(path) # Outputs the absolute path to README.md
To convert a path relative to the current file's directory:
from pydtl_relativepath import rel2abs, RelativePathType
path = rel2abs("..", "README.md", relative_type=RelativePathType.RELATIVE_TO_CURRENT_FILE)
print(path) # Outputs the absolute path to README.md located one directory up from the current file
To navigate up or down the directory structure while converting paths:
from pydtl_relativepath import rel2abs, RelativePathType
# Navigate up one directory from the current file's location
path = rel2abs("README.md", relative_type=RelativePathType.RELATIVE_TO_CURRENT_FILE, parent_dir_jump=-1)
print(path) # Outputs the absolute path to README.md located one directory up
The package is designed to raise informative errors for invalid inputs:
from pydtl_relativepath import rel2abs, RelativePathType
try:
path = rel2abs("README.md", relative_type="invalid_type")
except ValueError as e:
print(e) # Will print an error message regarding the invalid relative_type
Contributions are welcome! Please feel free to submit pull requests or open issues to improve the package or add new features.
See our Contribution Guide for more details.
pydtl-relativepath is released under the MIT License. See the LICENSE file for more details.