Skip to main content

Simplify your project entrypoint with @main

Project description

@main.py

mainpy - PyPI mainpy - Python Versions mainpy - license

mainpy - CI mainpy - pre-commit mainpy - basedpyright mainpy - ruff


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.main decorator 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:

@main(debug=True)
def app(): ...

Installation

The mainpy package is available on pypi for Python $\ge 3.8$:

pip install mainpy

Additionally, you can install the uvloop extra which will install uvloop>=0.14 (unless you're on windows):

pip install mainpy[uvloop]

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

mainpy-1.4.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

mainpy-1.4.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file mainpy-1.4.0.tar.gz.

File metadata

  • Download URL: mainpy-1.4.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.9.3-76060903-generic

File hashes

Hashes for mainpy-1.4.0.tar.gz
Algorithm Hash digest
SHA256 8b4763df867a20daef5226f7477a5b3a56d9f79ef9511f7e54d1f7be06a32de5
MD5 6d61053e567f000218a17eff89dc514f
BLAKE2b-256 420260b572ff3587599bff94f9cf87aa75f9ae8c0399a3c02fca17a445e21855

See more details on using hashes here.

File details

Details for the file mainpy-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: mainpy-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.9.3-76060903-generic

File hashes

Hashes for mainpy-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c2ea0cf65767f58bb053044e95b05998cdb5f44214783eff6a9dcaf05ecbeba
MD5 4e6e5bb47f132a5942d84b59f12d3773
BLAKE2b-256 c1d85991d85365a9f4c8208497b6a24db55d3d5804d2737dda4a06b43d383e44

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page