Simplify your project's main entrypoint definition with @main
Project description
@main
.py
Basic Examples
Instead of the verbose "boilerplate"
def main(): ...
if __name__ == '__main__':
main()
mainpy can be used to write it as:
from mainpy import main
@main
def app(): ...
Similarly, the async boilerplate
import asyncio
async def main(): ...
if __name__ == '__main__':
asyncio.run(main())
can be replaced with
@main
async def async_app(): ...
If, for some reason, you cannot or don't want to use a decorator, you can also call the decorator with the function as an argument:
import mainpy
def main(): ...
mainpy.main(main)
External Libraries
Click
With click
you can simply add the decorator as usual, as long as you keep in mind that it has to be the first decorator (i.e. above the @click.command()
):
import click
import mainpy
@mainpy.main
@click.command()
def click_command():
click.echo('Hello from click_command')
If you want to use @click.group
you probably don't want to decorate the group because the decorator will immediately execute. In those cases you probably want to move the mainpy.main(...)
execution to the bottom of the file:
import click
import mainpy
@click.group()
def group(): ...
@group.command()
def command(): ...
mainpy.main(group)
Typer
When using typer
you also need to use the regular call instead of the decorator:
import typer
import mainpy
app = typer.Typer()
@app.command()
def command():
typer.echo('typer.Typer()')
mainpy.main(app)
Automatic uvloop usage
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(): ...
Debug mode
Optionally, python's development mode
can be emulated by setting debug=True
in @main
. This will enable the
faulthandler,
configure the warnings
filter to display all warnings, and activate the
asyncio debug mode:
@main(debug=True)
def app(): ...
Installation
pip install mainpy
requires python > 3.7
Project details
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
File details
Details for the file mainpy-1.1.0.tar.gz
.
File metadata
- Download URL: mainpy-1.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.2 Linux/6.2.6-76060206-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56d58f285055999604dd27f87f0f1fe90e582d9affbda37f92ef3e43ac77604b |
|
MD5 | 45ad13f450b958f67769c6c07f8be99b |
|
BLAKE2b-256 | 8e20fb8857445ef80859e02eef28662c32a2d7ea560fe373dcf419614f37fd48 |
File details
Details for the file mainpy-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: mainpy-1.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.2 Linux/6.2.6-76060206-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ca1869bf7e2fe489d24fa08bbb4b0ffeb262cfbecd21aa8d52752aec48220b3 |
|
MD5 | 6dae5ce4a8db9eb18cd8171954f8a1dc |
|
BLAKE2b-256 | ca5a09bc2914fc1dd0541d6ca17123e8bf2c9dca6fb7ea83f1f3df4cc0d67074 |