Skip to main content

Simplify your project's main entrypoint definition with @main

Project description

@main.py


mainpy - pypi version mainpy - python versions mainpy - license mainpy - workflow status mainpy - basedpyright mainpy - ruff


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 async_app(): ...

if __name__ == '__main__':
    with asyncio.Runner() as runner:
        runner.run(async_app())

can be replaced with

from mainpy import main

@mainpy.main
async def async_app(): ...

If you cannot want to use a decorator, you can also call the decorator with the function as an argument:

def async_app(): ...

# do things before running async_app()

main(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.3.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

mainpy-1.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mainpy-1.3.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.5 Linux/6.8.0-76060800daily20240311-generic

File hashes

Hashes for mainpy-1.3.0.tar.gz
Algorithm Hash digest
SHA256 6c87990fbb27b042961d951fde70d5ae5da77bc4b31bd9a3dd7298bd3a135484
MD5 23be6ab7770b0d2370b38991767ca226
BLAKE2b-256 ad3d2f911fb436b429b7a4dc02e091d3c464455d118b276a5383b6db3b4dfb98

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mainpy-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.5 Linux/6.8.0-76060800daily20240311-generic

File hashes

Hashes for mainpy-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ef7f2fff19303ac8827c1c7ebbeca9f896ae29980f6a3df3ab60170d367adcc
MD5 c05ced233a0b874bfe775db79ab6874e
BLAKE2b-256 cfba9575a9209f0dd31182cf90ed3b2f9c47b2cb337305081babdbd6c0620440

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