1

I am not sure what I should be looking for with this question.

Say I've created a script in Python to parse command-line arguments. What should I do to make it executable from the command-line in the same manner as other *NIX tools are?

Ex: Instead of:

$ python program -s 2000

or

$ ./program -s 2000

I'd like to just be able to do the following:

$ program -s 2000

Ideally I'd like this to run on Windows and OSX. Any info on where I'd look to accomplish this? I have tried the shebang, which allows me to execute the script as per the second example, but I cannot find much information on allowing it to execute without specifying the current directory. Thanks!

anon_dev1234
  • 2,143
  • 1
  • 17
  • 33

3 Answers3

3

One thing that the other answers didn't pick up on so far: the difference between

./program ...

and

program ...

isn't something you can affect inside your program. It's because Unixen, by default, do not search the current directory when looking up an executable, only the directories on PATH. You need to either add . to PATH (not recommended because it's a security issue, and I don't actually know for sure it'd work), add the directory where program is to PATH, or install the program to a directory that already is there. (E.g. by symlinking it into /usr/local/bin/. I also use a combination of both for my utilities, putting ~/bin/ on PATH and putting / linking the programs there.)

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • to support `#!` shebang line on Windows, `pylauncher` could be installed. It will automatically configure file associations for Python files. See [How to execute Python scripts in Windows?](http://stackoverflow.com/a/13025018/4279). – jfs Mar 18 '13 at 23:12
  • So if I am following, you add a new directory to `PATH`, install the program there, and it is then able to be called since it is now in the lookup? – anon_dev1234 Mar 18 '13 at 23:12
  • @J.F.Sebastian The regular Python installer will do that anyway for `.py` and `.pyw` commands I think. Or at least support doubleclicking regardless of shebang line. (Although pylauncher seems like it might become the "builtin" way to do this sooner or later.) – millimoose Mar 19 '13 at 00:51
  • @bgoers That's pretty much all there's to it, as long as the script file is set as executable. – millimoose Mar 19 '13 at 00:55
  • @millimoose: true, if you only use a single python version. The shebang can be used to point to different python executables e.g., from different virtualenvs or Python versions. – jfs Mar 19 '13 at 01:12
  • @J.F.Sebastian Yeah. I guess my point is that it's useful, but possibly not necessary in the OP's case. – millimoose Mar 19 '13 at 01:15
1

Checkout this: http://www.pyinstaller.org/ . It looks like right tool for the job. Should work on Windows, OS X, Linux. Usage can be found here: https://github.com/pyinstaller/pyinstaller#usage

kkonrad
  • 1,262
  • 13
  • 32
1

Add the following line at the top of your file:

#!/usr/bin/python

and finally set the execution permission for your file

chmod +x program

The first line tells the program loader what interpreter should handle the file

jpmuc
  • 1,092
  • 14
  • 30
  • This is what I have done which currently lets me execute the program using `./program`. Ideally I'd like to just run it as `program` however. Does this allow the latter..? I am hoping I didn't miss something ha – anon_dev1234 Mar 18 '13 at 23:01
  • 1
    The fact that you need to preprend ./ has to do with security settings in UNIX environments. The idea is: the current directory does not belong to your PATH to avoid malicious programs located in your current folder and having the same name as common commands (i.e. cp) from being executed. If you REALLY want that, add "." to your PATH. – jpmuc Mar 18 '13 at 23:04
  • Gotcha. I'm not looking for anything that compromises security so.. good to know :) – anon_dev1234 Mar 18 '13 at 23:09