By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

executing python programs as if they were on OS path (noob)

P: n/a
Is there a place to put python programs so I dont have to refer to the
absolute path everytime I want to call them? For example:

if I am in /home/me and want to execute:

python /home/me/python/export.py temp.dat

what could I do so this will work:

python export.py temp.dat

Thanks,
Darren
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Darren Dale wrote:
Is there a place to put python programs so I dont have to refer to the
absolute path everytime I want to call them? For example:

if I am in /home/me and want to execute:

python /home/me/python/export.py temp.dat

what could I do so this will work:

python export.py temp.dat


At least two options (assuming you're on Linux... you
didn't specify your OS unfortunately):

1. Make the scripts executable (+x with chmod), make sure
the folder they are in *is* in the OS path, and put in the
following as the first line of the scripts:

#!/usr/bin/env python

2. Make your own shell script which knows where to look for your
Python files and execute that instead of "python". Call it "mypy"
or something to make sure you don't mess up anything else in the
system which wants the normal behaviour with "python".

Note that with your example above, you don't have to use the full
absolute path, as just "python python/export.py temp.dat" would
work.

-Peter
Jul 18 '05 #2

P: n/a
On Tue, 3 Aug 2004, Darren Dale wrote:
Is there a place to put python programs so I dont have to refer to the
absolute path everytime I want to call them? For example:

if I am in /home/me and want to execute:

python /home/me/python/export.py temp.dat

what could I do so this will work:

python export.py temp.dat


The easiest way to do this is to make the script executable directly:

1) Add the line '#!/usr/bin/env python' or '#!/path/to/python' (usually
/usr/bin/python or /usr/local/bin/python) to the top of your script, as
for a shell script.

2) Set your script's executable bit using 'chmod a+x'.

3) Stick your script, or a link to it, in something touched by the PATH
environment variable (such as /usr/local/bin) or, if you prefer, add your
script's directory to PATH (with a line like 'export
PATH=~/my/script/directory:$PATH' in your .bashrc file).

Then you can execute your script just by specifying its name.

Jul 18 '05 #3

P: n/a
>
At least two options (assuming you're on Linux... you
didn't specify your OS unfortunately):


I am currently on windows. I didnt think it would matter, sorry I left
that out.
Jul 18 '05 #4

P: n/a
On Tue, 3 Aug 2004, Darren Dale wrote:
At least two options (assuming you're on Linux... you
didn't specify your OS unfortunately):


I am currently on windows. I didnt think it would matter, sorry I left
that out.


You could probably still use Peter's second solution, but use a batch file
instead of a shell script.

Jul 18 '05 #5

P: n/a
Darren Dale wrote:
At least two options (assuming you're on Linux... you
didn't specify your OS unfortunately):


I am currently on windows. I didnt think it would matter, sorry I left
that out.


Ah, you tricked us by using forward slashes in the sample
path. :-)

On Windows, the best answer depends on *which* Windows you
are on... 98? XP? Other?

-Peter
Jul 18 '05 #6

P: n/a
Peter Hansen wrote:
Darren Dale wrote:
At least two options (assuming you're on Linux... you
didn't specify your OS unfortunately):

I am currently on windows. I didnt think it would matter, sorry I left
that out.

Ah, you tricked us by using forward slashes in the sample
path. :-)


I just wanted to pretend I was already a linux user...
On Windows, the best answer depends on *which* Windows you
are on... 98? XP? Other?


I'm on XP.
Jul 18 '05 #7

P: n/a
Darren Dale wrote:
I'm on XP.


Ah, then it's easy.

If you've installed the standard distribution properly, it very likely
is already set up so that merely typing the name of the Python script
on the command line will search in the directories in PATH for the
matching script, and execute it. In other words, just stop typing
"python" in front of the script name! :-)

And if you have the PATHEXT environment variable set up to contain
".py" then you won't even have to put .py on the end...

-Peter
Jul 18 '05 #8

P: n/a
Peter Hansen wrote:
Darren Dale wrote:
I'm on XP.

Ah, then it's easy.

If you've installed the standard distribution properly, it very likely
is already set up so that merely typing the name of the Python script
on the command line will search in the directories in PATH for the
matching script, and execute it. In other words, just stop typing
"python" in front of the script name! :-)

And if you have the PATHEXT environment variable set up to contain
".py" then you won't even have to put .py on the end...

-Peter


You know, I tried that the other day, and it didnt work. I think I know
the reason: when the DOS terminal loads, PATH is read. I didn't restart
the terminal after changing PATH.

Thanks for your help. Its working now.
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.