Skip to content
Myr

📝 Add docs for __main__.py (#121)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent 9b9c4e5 · Browse files · 1 file+158 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+158 added−1 removed

  • other:+158 added−1 removed

1 file+158 added−1 removed

modified:docs/tutorial/package.md+158 added-1 removed
423 unmodified lines
424```425426</div>427428## Support `python -m` (optional)429430You may have seen that you can call many Python modules as scripts with `python -m some-module`.431432For example, one way to call `pip` is:433434<div class="termy">435436```console437$ pip install fastapi438```439440</div>441442But you can also call Python with the `-m` *CLI Option* and pass a module for it to execute as if it was a script, like:443444<div class="termy">445446```console447$ python -m pip install fastapi448```449450</div>451452Here we pass `pip` as the value for `-m`, so, Python will execute the module `pip` as if it was a script. And then it will pass the rest of the *CLI Parameters* (`install fastapi`) to it.453454These two are more or less equivalent, the `install fastapi` will be passed to `pip`.455456!!! tip457 In the case of `pip`, in many occasions it's actually recommended that you run it with `python -m`, because if you create a virtual environment with its own `python`, that will ensure that you use the `pip` from *that* environment.458459### Add a `__main__.py`460461You can support that same style of calling the package/module for your own package, simply by adding a file `__main__.py`.462463Python will look for that file and execute it.464465The file would live right beside `__init__.py`:466467``` hl_lines="7"468.469├── poetry.lock470├── pyproject.toml471├── README.rst472├── rick_portal_gun473│ ├── __init__.py474│ └── __main__.py475└── tests476 ├── __init__.py477 └── test_rick_portal_gun.py478```479480No other file has to import it, you don't have to reference it in your `pyproject.toml` or anything else, it just works by default, as it is standard Python behavior.481482Then in that file you can execute your **Typer** program:483484```Python485from .main import app486app()487```488489Now, after installing your package, if you call it with `python -m` it will work (for the main part):490491<div class="termy">492493```console494$ python -m rick_portal_gun495496Usage: __main__.py [OPTIONS] COMMAND [ARGS]...497498 Awesome Portal Gun499500Options:501 --install-completion Install completion for the current shell.502 --show-completion Show completion for the current shell, to copy it or customize the installation.503504 --help Show this message and exit.505506Commands:507 load Load the portal gun508 shoot Shoot the portal gun509```510511</div>512513!!! tip514 Notice that you have to pass the importable version of the package name, so `rick_portal_gun` instead of `rick-portal-gun`.515516That works! 🚀 Sort of... 🤔517518See the `__main__.py` in the help instead of `rick-portal-gun`? We'll fix that next.519520### Set a program name in `__main__.py`521522We are setting the program name in the file `pyproject.toml` in the line like:523524```TOML525[tool.poetry.scripts]526rick-portal-gun = "rick_portal_gun.main:app"527```528529But when Python runs our package as a script with `python -m`, it doesn't have the information of the program name.530531So, to fix the help text to use the correct program name when called with `python -m`, we can pass it to the app in `__main__.py`:532533```Python534from .main import app535app(prog_name="rick-portal-gun")536```537538!!! tip539 You can pass all the arguments and keyword arguments you could pass to a Click application, including `prog_name`.540541<div class="termy">542543```console544$ python -m rick_portal_gun545546Usage: rick-portal-gun [OPTIONS] COMMAND [ARGS]...547548 Awesome Portal Gun549550Options:551 --install-completion Install completion for the current shell.552 --show-completion Show completion for the current shell, to copy it or customize the installation.553554 --help Show this message and exit.555556Commands:557 load Load the portal gun558 shoot Shoot the portal gun559```560561</div>562563Great! That works correctly! 🎉 ✅564565Notice that now it uses `rick-portal-gun` instead of `__main__.py` in the help.566567### Autocompletion and `python -m`568569Have in mind that TAB completion (shell auto-completion) won't work when using `python -m`.570571Auto-completion depends on the name of the program called, it's tied to each specific program name.572573So, to have shell completion for `rick-portal-gun` you would have to call it directly:574575<div class="termy">576577```console578$ rick-portal-gun [TAB][TAB]579```580581</div>582583But you can still support `python -m` for the cases where it's useful.584585## Publish to PyPI (optional)586587You can publish that new package to <a href="https://pypi.org/" class="external-link" target="_blank">PyPI</a> to make it public, so others can install it easily.588
206 unmodified lines
795You'll probably want to publish your code as open source to <a href="https://github.com/" class="external-link" target="_blank">GitHub</a>.796797And then you could integrate a <abbr title="Continuous Integration">CI</abbr> tool to run your tests and deploy your package automatically.798642And there's a long etc. But now you have the basics and you can continue on your own 🚀.799And there's a long etc. But now you have the basics and you can continue on your own 🚀.