Automatic path management for testing and development.
Project description
Table of Contents
What is autopypath?
autopypath is a small library that simplifies the management of the Python module search path (sys.path ) for testing and development environments. It automatically finds and adds relevant directories to sys.path based on configuration files such as pyproject.toml in the project’s root directory, works with popular testing frameworks like pytest and unittest, and supports standard project structures out of the box as well as custom configurations.
It does not use PYTHONPATH` from .env files because there is no way to reliably parse it in cross-platform environments and they are not designed for this purpose.
It is not a replacement for virtual environments but rather a tool to dynamically adjust sys.path in tests or scripts on load, making it easier to work with possibly broken build or development setups where the testing framework cannot be run because it attempts to import modules that are not yet installed or where other tests cannot be loaded due to breakage from ongoing development.
It works by detecting the root of the project repository using common version control system markers (such as .git, .hg, or .svn directories, or a pyproject.toml file, or a custom configuration file (autopypath.toml), and then adding specified subdirectories (such as src/, src/tests/, tests/, lib/ or others defined in configuration files) to sys.path at runtime. The detection of the repository root and the addition of paths to :sys.path happens automatically when autopypath is imported in a test script. It is fully customizable via configuration files or parameters.
This allows test scripts to be executed directly from the command line or an IDE without requiring the entire project to be built or installed first as long as its dependencies are met.
It is designed to be lightweight and easy to integrate into existing testing setups, providing a seamless experience for developers working on Python projects and to “just work” without manually worrying about the intricacies of path resolution and IDE or test runner configurations except for the initial setup.
Usage
This is an example of how to use autopypath in your test scripts so that a single test can be run directly from the command line or an IDE without requiring the entire test suite to be executed even if the project is not fully built or installed as long as its dependencies are met .
Even if your test script is located deep in a repo test subdirectory, you can still use autopypath to ensure that the necessary paths are added to sys.path without any manual intervention.
Here is an example of how a test script named my_test_script.py might look after integrating autopypath (and assuming the project structure uses a pyproject.toml or has configured an autopypath.toml file if the project structure is non-standard):
import autopypath
import pytest
import mypackage.my_module
from mypackage.subpackage import my_other_module
### my tests
...
if __name__ == '__main__':
pytest.main([__file__])
It can then be run directly from the CLI or from your IDE without having to install the package first, run the full test suite, or manually mangling sys.path, or PYTHONPATH to make it work.
python my_test_script.py
It automatically resolves the paths based on the configuration files and adds them to sys.path before running the tests.
Configuring
If you already use pyproject.toml to define your project structure, autopypath has first-class support for it and will automatically detect it and use a [tool.autopypath] section if present to configure itself.
Example:
[tool.autopypath] paths = ['lib', 'src/tests', '.']
If you do not use pyproject.toml or want to have a separate configuration file, you can create an autopypath.toml file in the root of your project or in any parent directory containing your scripts to configure autopypath.
It is cross-platform and works reliably on Windows, macOS, and Linux.
Example:
[tool.autopypath] repo_markers = {".git" = "dir"} paths = ["src", "src/lib", "tests"]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autopypath-0.9.0.tar.gz.
File metadata
- Download URL: autopypath-0.9.0.tar.gz
- Upload date:
- Size: 231.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebba8b328545dae5f0941cc139794e1820b1fa1d49011788a3f694636c0324b6
|
|
| MD5 |
1f30db0a618d5832374d0d7f97117fe6
|
|
| BLAKE2b-256 |
7c894bb397eaae52ca90867b0af3c9479acc518b74c711f5b0b409d4e4cd4525
|
File details
Details for the file autopypath-0.9.0-py3-none-any.whl.
File metadata
- Download URL: autopypath-0.9.0-py3-none-any.whl
- Upload date:
- Size: 49.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4da2a0e43bc904a49be9088522435a66bf2ab8a0fb3bc03833d76a41cde84da0
|
|
| MD5 |
276951ddb71e81421426f68f61dc5ec6
|
|
| BLAKE2b-256 |
b7bb216014ad553e044010067ed5ee8a83df8a70a47ecb217805c65d26d08c5f
|