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
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!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With