Python, run package with `python3.6 -m somepackge.run`

Viewed 9248

I'd like to do the same I can do with python3.6 -m http.server. I'd like to run my oneliner like that. How do I do this? For now I've got:

def run():
    print('Great!')


if __name__ == '__main__':
    run()

I tried python3.6 -m fastapi and I've got /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6: No module named test.__main__; 'test' is a package and cannot be directly executed,

and with python3.6 -m fastapi.run /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6: No module named test.run

1 Answers

You need to define a magic file, called __main__.py within your module. See the Python 3 docs on __main__.

In there you typically run a single main() entrypoint function.

For examples, look at pip's, or Tox's one.

Related