473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
8 1549
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
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
>
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.