Simplify your project entrypoint with @main
Project description
@main.py
Basic Examples
With mainpy, there's no need to write if __name__ == '__main__' the
boilerplate anymore:
without mainpy |
with mainpy |
|---|---|
if __name__ == '__main__':
app()
def app(): ...
|
from mainpy import main
@main
def app(): ...
|
For async apps, the improvement becomes even more obvious:
without mainpy |
with mainpy |
|---|---|
import asyncio
async def async_app(): ...
if __name__ == '__main__':
with asyncio.Runner() as runner:
runner.run(async_app())
|
from mainpy import main
@main
async def async_app(): ...
|
External Libraries
Even though mainpy requires no other dependencies than typing_extensions
(on Python < 3.10), it has optional support for uvloop, and plays
nicely with popular CLI libraries, e.g. click and typer.
uvloop
If you have uvloop installed, mainpy will automatically call
uvloop.install() before running your async main function.
This can be disabled by setting use_uvloop=False, e.g.:
@main(use_uvloop=False)
async def app(): ...
Click
With click you can simply add the decorator as usual.
[!IMPORTANT] The
@mainpy.maindecorator must come before@click.command().
import mainpy
import click
@mainpy.main
@click.command()
def click_command():
click.echo('Hello from click_command')
The function that is decorated with @mainpy.main is executed immediately.
But a @click.group must be defined before the command function.
In this case, mainpy.main should be called after all has been setup:
import mainpy
import click
@click.group()
def group(): ...
@group.command()
def command(): ...
mainpy.main(group)
Typer
A typer internally does some initialization after a command
has been defined.
Instead of using @mainpy.main on the command itself, you should use
mainpy.main() manually:
import mainpy
import typer
app = typer.Typer()
@app.command()
def command():
typer.echo('typer.Typer()')
mainpy.main(command)
Debug mode
Optionally, Python's development mode can be emulated by passing
debug=True to mainpy.main. This does three things:
- Enable the faulthandler
- Configure
warningsto display all warnings - Runs
asyncfunctions in debug mode
@main(debug=True)
def app(): ...
Installation
The mainpy package is available on pypi for Python $\ge 3.10$:
pip install mainpy
Additionally, you can install the uvloop extra which will install
uvloop>=0.15.2 (unless you're on windows):
pip install mainpy[uvloop]
Project details
Release history Release notifications | RSS feed
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 mainpy-1.5.0.tar.gz.
File metadata
- Download URL: mainpy-1.5.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6083d34ad0f3e0fa07e144f03c9dae32b0fa1554b0213053dd19994c0a169fd8
|
|
| MD5 |
227453f397fe2cbfe8cebe10288fb0fd
|
|
| BLAKE2b-256 |
d0d6e194355f8df0caee4124226308cb5bbfe4f502378cc3ec531e5d4c508144
|
Provenance
The following attestation bundles were made for mainpy-1.5.0.tar.gz:
Publisher:
publish-pypi.yml on jorenham/mainpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mainpy-1.5.0.tar.gz -
Subject digest:
6083d34ad0f3e0fa07e144f03c9dae32b0fa1554b0213053dd19994c0a169fd8 - Sigstore transparency entry: 590949198
- Sigstore integration time:
-
Permalink:
jorenham/mainpy@886d47bf113447f6f47df265a1fc81beed4343d5 -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/jorenham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@886d47bf113447f6f47df265a1fc81beed4343d5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mainpy-1.5.0-py3-none-any.whl.
File metadata
- Download URL: mainpy-1.5.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6575b02777a7e8e7183197bf45192a2b4dcdd01bd2c2811ac8f800c455bbc3ed
|
|
| MD5 |
9a12150bc42e68187b311ac0f813567a
|
|
| BLAKE2b-256 |
56a661defba8bd47df1ff3935a83b4e2543f446e4450d2686b3f0429e92065a7
|
Provenance
The following attestation bundles were made for mainpy-1.5.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on jorenham/mainpy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mainpy-1.5.0-py3-none-any.whl -
Subject digest:
6575b02777a7e8e7183197bf45192a2b4dcdd01bd2c2811ac8f800c455bbc3ed - Sigstore transparency entry: 590949204
- Sigstore integration time:
-
Permalink:
jorenham/mainpy@886d47bf113447f6f47df265a1fc81beed4343d5 -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/jorenham
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@886d47bf113447f6f47df265a1fc81beed4343d5 -
Trigger Event:
release
-
Statement type: