Python script invocation weirdness on Windows (aka why PATH did not get inherited?)

137 views Asked by At

I've encountered a behavior I did not expect when executing the following Python script (I named it repro.py):

import os
print os.environ['PATH'].replace(';', '\n')

If invoked by python repro.py, I get this:

C:\Python26\
C:\Python26\Scripts
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Program Files\MySQL\MySQL Server 5.1\bin

When I invoke it by entering repro.py on the command prompt, I get this result:

C:\Python26\

Clearly repro.py inherits different environments depending on how it was started. When started as the first arg to python interpreter, it behaves more like I'd expect. On the other hand, when it gets started by whatever process that automatically figures out what program to use when "opening" .py files, it inherits really austere environment. It begins to smell like some sort of security feature.

Is there any way to make both ways of invocation behave the same, preferably equivalent to python repro.py? Does anyone know why this is happening?

Version info: ActivePython 2.6.5.12 (ActiveState Software Inc.) on Windows7.

1

There are 1 answers

1
tangentstorm On BEST ANSWER

That is bizarre. I'm running the standard python 2.7 from python.org on windows 7, and both invocations print the full PATH, as expected.

It may be a bug, or "feature" in ActivePython. :)

Although, are you opening them both in the same command prompt window? Perhaps your path really had been changed?