Skip to main content

Python Import Guardian README

What is it?

python-import-guardian is a static-analysis import guardian for Python. It can be used to control explictly which Python modules and packages can be imported by a given set of modules and packages in your project.

When is this useful?

Let’s say you have a project with two major components — a client and a server. Both of these components live in the same repository (for the sake of argument) and both use a common set of shared code for various functions — for example to determine how certain hashes are calculated or some such.

The code may be laid out thus:

/myproject
  |
  |- client/
  |- server/
  |- shared/

Conceptually, you only ever want client and shared code to be installed on client machines and server and shared code on server machines. Modules in client and server may import from shared, but not from each other, and shared may import from neither — this ensures that you have a good separation of concerns between client and server code whilst maintaining some shared code and good DRY principles.

python-import-guardian allows you to define how you want these modules to relate to each other when it comes to which module can import what from where. For our example project, a simple importguardian.json file at the top of the project tree might look like this:

{
    "forbidden_modules": {
        "server": [
            "client",
            "shared"
        ],
        "client": [
            "server",
            "shared"
        ]

    }
}

The forbidden_modules declaration here maps Python modules to a list of the modules or packages which may not import them. So in this case: server may not be imported by client or shared, and client may not be imported by server or shared. forbidden_modules is a blacklist, so shared can be imported by anything because it isn’t mentioned as a forbidden module.

Metadata

Release files for python-import-guardian 0.2.3

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

Source distribution (sdist)

Source distribution for python-import-guardian 0.2.3
File Size Uploaded Provenance Actions
python_import_guardian-0.2.3.tar.gz 10.3 kB Without provenance Details for python_import_guardian-0.2.3.tar.gz

Release files / python_import_guardian-0.2.3.tar.gz

Download URL python_import_guardian-0.2.3.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bedb259ba94cde1132c5d3e3b313541235d87a4dddb063cfb5eef8c27852280f
BLAKE2b-256 checksum
How to use checksums
26740c531e1e1f3e094625523d29bebcacba957a97c5c084dd8befe53413f5da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.4

Security for python-import-guardian 0.2.3

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