0

I want to call a python script from batch script, but I dont want to hard-code path to python executable (python.exe) in my calling script.

e.g.

c:\python26\python.exe test.py
$PYTHONPATH\python.exe test.py

Is there any way to have PYTHONPATH like setting ?

Dietrich Epp
216k39 gold badges366 silver badges427 bronze badges
asked Jan 29, 2010 at 16:41
2
  • why don't you just call it test.py. or it doesn't work for you? Commented Jan 29, 2010 at 16:45
  • @SilentGhost The question should have been rather Why don't you call python test.py? as this is the usual way to call Python script on Windows. Counting on right association of Python files is much more fragile. Commented Oct 15, 2011 at 18:22

3 Answers 3

4

The simplest thing is to add c:\python26 to you system's PATH.

Also, depending on how you installed Python, you should be able to just use test.py on the command line.

answered Jan 29, 2010 at 16:46
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't python installation do any such setting? Because I will not know on which setup will my script run.
Some Python installers do, but I don't know if you can count on it.
3
set PYTHON_INSTALL=D:\python26

then:

%PYTHON_INSTALL%\python.exe test.py

You could set up the PYTHON_INSTALL var using My Computer | Advanced | Environment Variables if you want it to persist.

EDIT: And building on the other post (put the path to Python in the system path), you could have the best of both worlds:

set PATH=%PATH%;%PYTHON_INSTALL%

Then you can just call:

python test.py

EDIT 2:

Renamed 'PYTHONPATH' to 'PYTHON_INSTALL' as another poster pointed out that the environment variable 'PYTHONPATH' already has a defined use.

answered Jan 29, 2010 at 16:46

2 Comments

Don't use PYTHONPATH for this, as it already serves a different purpose (telling Python about extra directories to add to its module search path.)
And I think 'set PATH' should use ';' as the separator.
0

Try

set PYTHONPATH=c:\python26
%PYTHONPATH%\python.exe test.py

Or

set PATH=%PATH%;C:\python26;
python test.py

Note: environment variable PYTHONPATH has different purpose for searching python modules/extensions, So should not be shadowed.

PYTHONPATH : ';'-separated list of directories prefixed to the
 default module search path. The result is sys.path.
answered Jan 29, 2010 at 16:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.