-1

I would like apologise since this is not exactly a programming releated question but rather something I would like to know :

I installed a python library with files say: lib.py, lib2.py .. lib-n.py. All these scripts take command line arguments when being called. So it looks like

username@machinename:~$ lib.py -s <args> -t <args> ..

Now like you can see above, I can run these scripts form any directory and without using the 'python' keyword before calling them. I would like to do this with the python scripts that I write as well. ie; I should be able to call them from any directory instead of 'cd'ing to their location.

P.S: Using a Linux machine running Ubuntu 12.04 and python 2.7.3

lets_try
  • 15
  • 1
  • 6

1 Answers1

3

Add this to top of your script -

#!/usr/bin/env python

And then make your python script executable using chmod -

chmod u+x <python script>

Also, if you do not want to give complete path to python script, you can add the directory the script exists in to PATH environment variable.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • 2
    ... and add that dir to `$PATH`. – bereal Jun 20 '15 at 07:29
  • To be picky, the variable is called `PATH`, `$PATH` gives the value (`$` being a unary operator). Some consider the use of `/usr/bin/env` to be a security risk (because environment variables can be compromised, e.g. "shellshock"), and a safer `chmod` is to use `u+x`. – cdarke Jun 20 '15 at 07:53
  • but if we do not use `/usr/bin/env` and instead use complete path to python, it may not work if python got moved – Anand S Kumar Jun 20 '15 at 07:56