execute script everywhere

Paul Watson pwatson at redlinec.com
Sat Mar 13 23:30:26 EST 2004


"myang" <myang at clarku.edu> wrote in message
news:mailman.12.1079231052.745.python-list at python.org...
>
> After we compile a C program, we can put the executable file under a
> directory (like /bin) so that we can run it everywhere, and don't have to
> type the full path.
> Can we do the same thing to a python script?
> For example, I have a script.py under "/home/john/". Can I just type
"python
> script.py" to run it under another directory "/home/tom/"?
> Thanks!
>
> Yang

It appears that you are on a UNIX system.  Most systems will support
"shebang" proceesing of the first line in the file.  The first line tells
UNIX to find 'python' in the PATH and run it to exectute this script.  Use
'man env' for more information.

#! /usr/bin/env python
import os
import sys
...

When you do this, you do not need to uses the word 'python' on the command.
At the '$' prompt, just use:

script.py

The first line tells it to use Python to process the file.  Of course
/home/john where the script.py file exists must be in the PATH variable in
order to run it from anywhere.  Just like you have /bin in your PATH
variable already.





More information about the Python-list mailing list