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

D

Darren Dale

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
 
P

Peter Hansen

Darren said:
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
 
C

Christopher T King

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.
 
D

Darren Dale

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.
 
C

Christopher T King

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.
 
P

Peter Hansen

Darren said:
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
 
D

Darren Dale

Peter said:
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.
 
P

Peter Hansen

Darren said:
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
 
D

Darren Dale

Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top