469,622 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

execute script everywhere


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
Jul 18 '05 #1
2 2456
myang wrote:
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/"?


Make sure it has a proper bangpath, make it executable, and then put it
somewhere in your PATH. Then you can run it no matter what your pwd is
with

script.py

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Courage is the fear of being thought a coward.
-- Horace Smith
Jul 18 '05 #2
"myang" <my***@clarku.edu> wrote in message
news:ma***********************************@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.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tomasz Ludwiniak | last post: by
30 posts views Thread by Toni Mcintyre | last post: by
3 posts views Thread by Lyle Fairfield | last post: by
4 posts views Thread by Chr1s | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.