-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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

... and add that dir to $PATH.
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.
but if we do not use /usr/bin/env and instead use complete path to python, it may not work if python got moved

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.