Guilherme Salome
Guilherme Salome

Reputation: 2039

Python and the PATH environment variable in Windows

I am following the Pylearn2 tutorial and in one of the steps the following is written:

You should have pylearn2/scripts in your PATH enviroment variable.

So i added:

C:\Anaconda\Lib\site-packages\pylearn2-0.1dev-py2.7.egg\pylearn2\scripts\

to the PATH variable.

If i want to execute one of the scripts that is in the mentioned folder (for example 'train.py') by using the 'execfile' function, do i need to add the path to it again? I have been trying this in the interpreter:

>>> execfile('train.py')

However, i get the error message:

IOError: [Errno 2] No such file or directory: 'train.py'

Shouldn't python look for the script in the directory path in the PATH variable?

Please help me if you can.

Upvotes: 0

Views: 1123

Answers (1)

abarnert
abarnert

Reputation: 365587

No, execfile does not search the PATH. It just takes a normal filename (which can be relative or absolute) and opens it exactly the same as any other file-handling function.

On top of that, you very rarely want to use execfile. In this particular case, what you should probably be doing is running the script from the cmd ("DOS box") prompt, not the Python prompt.

If you really want to use the Python prompt as your "shell" in place of cmd, you can do that, but you still want to be able to find programs via the PATH, run them in a separate interpreter instance, etc. The way to do that is with subprocess. For example:

>>> from subprocess import check_call # you only have to do this once
>>> check_call(['train.py'])

That's a lot more typing than you need to do from cmd, of course:

C:\> train.py

… but then you can't run arbitrary Python statements at cmd, so there's a tradeoff.

Upvotes: 1

Related Questions