Skip to main content

pathimport

This simple python module allows to elegantly handle project imports on python without headaches.

Why pathimport?

Using import statements inside python projects can lead to numerous headaches and subtle problems. Importing in python requires a deep understanding of how the interpreter loads other modules and scripts on runtime. A misuse of imports can lead to problems when your project is deployed outside your development environment; and even before that, imports can hide many pitfalls. This package wants to simplify the life of python users, allowing them to perform imports from other scripts of the same project effortlessly.

Installation

The pathimport module can be installed through pip.

pip install pathimport

Usage

Let's suppose to have a python project with the following structure.

module
├── submodule_a                            
│   └── script_a.py                    
├── submodule_b
│   └── script_b.py
└── common.py

Inside common.py we have defined the following function,

def fun_common():
    # do things...

while in script_a.py we have defined the following.

def fun_a():
    # do things...

To avoid the usage of relative imports, pathimport can be used to load these functions inside script_b.py.

from pathimport import set_module_root
set_module_root("..")

# from now on all the imports can be
# perfomed from the module root
from common import fun_common
from submodule_a import fun_a

if __name__ == "__main__":
    fun_common()
    fun_a()

Optionally, the module/project name can be used as a prefix.

from pathimport import set_module_root
set_module_root("..", prefix=True)

from module.common import fun_common
from module.submodule_a import fun_a

Under The Hood

To resolve the interpreter import ambiguities, set_module_root of pathimport modifies the PATH environment variable to point to the module root (or to the preceding directory if prefix is set to True). For this reason, the interpreter will be able to correctly localize and import your scripts from the filesystem, without additional problems that can arise depending on the current working directory, or if the source code is launched as a script or as a module.

Additional Resources

Metadata

Release files for pathimport 0.0.4

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

Source distribution (sdist)

Source distribution for pathimport 0.0.4
File Size Uploaded Provenance Actions
pathimport-0.0.4.tar.gz 2.8 kB Without provenance Details for pathimport-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pathimport 0.0.4
File Interpreter ABI Platform Actions
pathimport-0.0.4-py3-none-any.whl Python 3 none any Details for pathimport-0.0.4-py3-none-any.whl

Total release size: 5.8 kB

Release files / pathimport-0.0.4.tar.gz

Download URL pathimport-0.0.4.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
91e9afee42f604c16400edb87cf7546d0233f6564feb10409aead3b8473c4168
BLAKE2b-256 checksum
How to use checksums
af97e688bb12a981f93f24655d6f713971b464540c19096db6e5d8b3ed9f90b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / pathimport-0.0.4-py3-none-any.whl

Download URL pathimport-0.0.4-py3-none-any.whl
Size 3.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fd14ca376209c06d01fc1a73e7365fa6dda989118d17b942e6e8caa8cfe530d
BLAKE2b-256 checksum
How to use checksums
49cc241167f11f4e2454bc2ae68ac82e00e65c07f474e825d42f5add304eb7e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Security for pathimport 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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