Skip to main content

set-package-attribute

Run modules inside packages as scripts. Automatically sets the __package__ attribute of any script which imports it and calls the initialization function set_package_attribute.init(). This allows the script to use explicit relative imports, which fail otherwise.

For full documentation, see https://abarker.github.io/set-package-attribute.

Also available on PyPI for installation with pip.

Brief usage summary

To use the package just import it before any of the non-system files, inside any module that you might want to run as a script, and call the init function. These statements should be inside a guard conditional, so that they only run when the module is executed as a script:

if __name__ == "__main__":
    import set_package_attribute
    set_package_attribute.init()

The guard conditional is not required if the module is only ever run as a script, i.e., if it is never imported by any module. The init function must be called before any explicit relative imports, and before importing any modules from within the same package which themselves use such imports.

If you are happy with the default init arguments then there is a shortcut import which automatically calls init for you:

if __name__ == "__main__":
    import set_package_attribute_magic

Metadata

Release files for set-package-attribute 0.2.5

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

Source distribution (sdist)

Source distribution for set-package-attribute 0.2.5
File Size Uploaded
set-package-attribute-0.2.5.tar.gz 7.8 kB Details

Release files / set-package-attribute-0.2.5.tar.gz

Download URL set-package-attribute-0.2.5.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
da1de44d1122563dab0a65e1286ae37b723ef823b5165e736b4153a7b5decb3d
BLAKE2b-256 checksum
How to use checksums
23e2fce046df45a3b795cb99849a2e74a300cacb2d56a7c5e5627a7a005d4a57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.8

Release history Release notifications | RSS feed

This release

0.2.5 This release

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

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