I want to make a python package executable from the command line.
I know you can do chmod +x myfile.py
where myfile.py
starts with #!/usr/bin/env
to make a single file executable using ./myfile.py
. I also know you can do python -m mypackage
to run a package including a __main__.py
.
However, if I add the shebang line to the __main__.py
of a package, run chmod +x mypackage
, and try ./mypackage
, I get the error -bash: ./mypackage: Is a directory
.
Is it possible to run a package like this?
(To be clear, I'm not looking for something like py2exe to make it a standalone executable. I'm still expecting it to be interpreted, I just want to make the launch simpler)
Short answer is No .
When you make chmod +x mypackage
you are doing nothing because mypackage
is a directory and directories already has execute flag (or you will be unable to list their files). If you type: ls -l
you will see.
Your options to run directly the whole package without installing it is the way you already mention: python -m mypackage
, or make a shell script which will do that for you.
I see that your intentions are to execute just ./something
and your application to start working without specifying python
in front and also this to not be globally installed. The easyest way will be to put a shell script that will launch your package.
This contains what you are looking for. Walks you through how to setup your package so that a commandline application/interface is possible.
Would suggest using argparse instead of sys.argv. The post's comments are also helpful for more updated tips.
https://gehrcke.de/2014/02/distributing-a-python-command-line-application/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.