1

I have written a Python program to analyse a set of files present in a given directory, i.e. it asks for a path then it goes into the path directory and find specified files, process them and produces an output file. However, every time I want to execute the program on a UNIX machine I have to write python my_prog.py. Also, to process a directory the program must be copied to the directory first, then executed.

I want to make it so that in UNIX I type my_prog inside any directory the program be executed, which means getting rid of copying the program file to target directory. How can I do this?

2 Answers 2

9

Make your program executable with

chmod +x my_prog.py

put

#!/usr/bin/env python

(or a variation of this) at the top of your source file and place your my_prog.py script into a directory in your path (i.e., somewhere in the set of directories as usually defined by the PATH environment variable which is searched for commands to be executed).

Once your program is in your path, you will be able to execute it from anywhere, i.e., without having to place the program in your current directory or fully specifying the directory path to it.

You should be able to see the value of your current PATH environment variable in most shells with this command:

env | grep -i path

or

echo $PATH

Just in case, here is a bit more basic information on the PATH variable: http://www.cs.purdue.edu/homes/cs348/unix_path.html and http://kb.iu.edu/data/acar.html. Google will yield many more. Hope this helps.

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

3 Comments

but as i've told i just want to avoid copying the prog file to the directory in my path
Not your path as in your current working directory, your path as in the set of locations like /usr/bin that you can execute commands from. Copy it to one of those locations once, and you'll be able to run it anywehere.
@Marius is correct, and I appended some additional explanation to my original answer. Hope that helps.
2

As always, add a shebang line, make it executable, and copy it to a directory in $PATH.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.