Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 861
Meroz Avatar asked Nov 03 '18 10:11

Meroz


People also ask

How do I run a python3 package?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do I call a package in Python?

First, we create a directory and give it a package name, preferably related to its operation. Then we put the classes and the required functions in it. Finally we create an __init__.py file inside the directory, to let Python know that the directory is a package.

How do I run a Python pip package?

Ensure you can run pip from the command lineRun python get-pip.py . 2 This will install or upgrade pip. Additionally, it will install setuptools and wheel if they're not installed already. Be cautious if you're using a Python install that's managed by your operating system or another package manager.


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.

like image 141
declension Avatar answered Oct 01 '22 04:10

declension