6

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)

martineau
  • 119,623
  • 25
  • 170
  • 301
ericksonla
  • 1,247
  • 3
  • 18
  • 34
  • 1
    You have to run a *file*, not a directory. What happens with running ./mypackage/__main__.py ? Python may know how to find the main program, but the Linux shell does not. – Prune Feb 03 '17 at 21:38
  • 2
    *Is it possible to run a package like this?* <= no. But why not drop a shell script? – dhke Feb 03 '17 at 21:38
  • 4
    You might want to look into the [setuptools `console_scripts` entry point](http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point). *(Assuming your package is a proper `setuptools` distribution, containg a `setup.py` etc..)* – Lukas Graf Feb 03 '17 at 21:38
  • see: http://stackoverflow.com/questions/6630822/running-a-python-package and http://stackoverflow.com/questions/4050120/execute-an-installed-python-package-as-a-script `python -m module.__main__` – ScottSmudger Feb 03 '17 at 21:39

2 Answers2

1

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.

VStoykov
  • 1,705
  • 12
  • 15
1

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/

colbythenoob
  • 95
  • 2
  • 9
  • A link to an answer is not an answer; to be on-topic here, an answer needs to be complete enough to be helpful _even if all the links it contains break_. – Charles Duffy Aug 21 '22 at 20:50
  • See [Your answer is in another castle: When is an answer not an answer?](https://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer) on [meta.se]. – Charles Duffy Aug 21 '22 at 20:51