📝 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
Provenance
- Commit
fe60816- Tree
f02be3d- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
9b9c4e5
1 file changed+158 added−1 removed
modified:docs/tutorial/package.md+158 added-1 removed
423 unmodified lines424 ```425 426 </div>427 428+## Support `python -m` (optional)429+430+You may have seen that you can call many Python modules as scripts with `python -m some-module`.431+432+For example, one way to call `pip` is:433+434+<div class="termy">435+436+```console437+$ pip install fastapi438+```439+440+</div>441+442+But 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:443+444+<div class="termy">445+446+```console447+$ python -m pip install fastapi448+```449+450+</div>451+452+Here 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.453+454+These two are more or less equivalent, the `install fastapi` will be passed to `pip`.455+456+!!! 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.458+459+### Add a `__main__.py`460+461+You can support that same style of calling the package/module for your own package, simply by adding a file `__main__.py`.462+463+Python will look for that file and execute it.464+465+The file would live right beside `__init__.py`:466+467+``` 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+```479+480+No 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.481+482+Then in that file you can execute your **Typer** program:483+484+```Python485+from .main import app486+app()487+```488+489+Now, after installing your package, if you call it with `python -m` it will work (for the main part):490+491+<div class="termy">492+493+```console494+$ python -m rick_portal_gun495+496+Usage: __main__.py [OPTIONS] COMMAND [ARGS]...497+498+ Awesome Portal Gun499+500+Options: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.503+504+ --help Show this message and exit.505+506+Commands:507+ load Load the portal gun508+ shoot Shoot the portal gun509+```510+511+</div>512+513+!!! tip514+ Notice that you have to pass the importable version of the package name, so `rick_portal_gun` instead of `rick-portal-gun`.515+516+That works! 🚀 Sort of... 🤔517+518+See the `__main__.py` in the help instead of `rick-portal-gun`? We'll fix that next.519+520+### Set a program name in `__main__.py`521+522+We are setting the program name in the file `pyproject.toml` in the line like:523+524+```TOML525+[tool.poetry.scripts]526+rick-portal-gun = "rick_portal_gun.main:app"527+```528+529+But when Python runs our package as a script with `python -m`, it doesn't have the information of the program name.530+531+So, 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`:532+533+```Python534+from .main import app535+app(prog_name="rick-portal-gun")536+```537+538+!!! tip539+ You can pass all the arguments and keyword arguments you could pass to a Click application, including `prog_name`.540+541+<div class="termy">542+543+```console544+$ python -m rick_portal_gun545+546+Usage: rick-portal-gun [OPTIONS] COMMAND [ARGS]...547+548+ Awesome Portal Gun549+550+Options: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.553+554+ --help Show this message and exit.555+556+Commands:557+ load Load the portal gun558+ shoot Shoot the portal gun559+```560+561+</div>562+563+Great! That works correctly! 🎉 ✅564+565+Notice that now it uses `rick-portal-gun` instead of `__main__.py` in the help.566+567+### Autocompletion and `python -m`568+569+Have in mind that TAB completion (shell auto-completion) won't work when using `python -m`.570+571+Auto-completion depends on the name of the program called, it's tied to each specific program name.572+573+So, to have shell completion for `rick-portal-gun` you would have to call it directly:574+575+<div class="termy">576+577+```console578+$ rick-portal-gun [TAB][TAB]579+```580+581+</div>582+583+But you can still support `python -m` for the cases where it's useful.584+585 ## Publish to PyPI (optional)586 587 You 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 lines795 You'll probably want to publish your code as open source to <a href="https://github.com/" class="external-link" target="_blank">GitHub</a>.796 797 And then you could integrate a <abbr title="Continuous Integration">CI</abbr> tool to run your tests and deploy your package automatically.798 642-And there's a long etc. But now you have the basics and you can continue on your own 🚀.799+And there's a long etc. But now you have the basics and you can continue on your own 🚀.