Skip to main content

@python_main

PyPI - Python Version PyPI - Version

@python_main is a decorator which:

  • Automatically calls the function(s) tagged with it, if the current module is being executed as a script.
  • Does nothing if the current module is being imported.

It is, essentially, equivalent to the if __name__ == "__main__": construct, but as a decorator.

That's all it does.

Installation

pip install python-main # or
poetry add python-main # ...

Usage

from python_main import python_main

A = 10
B = 20

@python_main
def do_print():
    """This will run if this module is executed."""
    print(A + B)

You can also tag multiple functions with @python_main and they will all run if the module is executed, in the order they are defined.

from python_main import python_main

A = 10
B = 20
C = 0

@python_main
def add_a_to_c():
    global C
    C += A

# ... other functions/ definitions ...

@python_main
def add_b_to_c():
    global C
    C += B

# At this point:
# - C will be 30 if this module is executed as a script.
# - C will be untouched if this module is imported.

Metadata

Release files for python-main 2.0.0

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-main 2.0.0
File Size Uploaded
python_main-2.0.0.tar.gz 1.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-main 2.0.0
File Interpreter ABI Platform
python_main-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 4.0 kB

Release files / python_main-2.0.0.tar.gz

Download URL python_main-2.0.0.tar.gz
Size 1.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7f38764d52bac3b4edd21b78528144b0660acc0924c275e97f9a33418765b1ba
BLAKE2b-256 checksum
How to use checksums
511b1a75c9e76e7eaffb1f6eacc6c3902335a79809b5994b13babed97e2bf70d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 7, 2024.

Transparency log

Release files / python_main-2.0.0-py3-none-any.whl

Download URL python_main-2.0.0-py3-none-any.whl
Size 2.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5727b2825f19f9a154936fc41a780f65caf6312518face47bfb890345c07ae52
BLAKE2b-256 checksum
How to use checksums
413bdfaf6ebf2d09f14489f04ab47fc1f23bc7b75d11a55a54679ca251af01e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 7, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

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