We help IT Professionals succeed at work.

Python for Windows Script Won't run from Batch File

JohnnyD74 asked
Hello Folks,

I installed Python 2.5 on a windows server and I copied a script called "check_esxi_hardware.py" to "C:\Python25". I can run the script just fine when called from DOS command line when I type it in manually but when I copy the exact same command from notepad into command window, it errors out. I tried running the script though a text cleaner called "stripmail" it revealed a space between two characters. I deleted the space and when I then copied it into command line, it worked. This techniqe does not work however if I copy to a batch file and run it which is the goal.

The Command is:

"c:\python25\check_esxi_hardware.py" –H esxhost –U root –P mypasswd –V hp  



Watch Question

HonorGodSoftware Engineer

What you need to do is something like:

The program being executed is the python interpreter
"c:\python25\bin\python.exe" "c:\python25check_esxi_hardware.py" –H esxhost –U root –P mypasswd –V hp

Open in new window



Thanks very much for your reply but the execuable (python.exe) is in in the "c:\python25" folder so I would not need to call it seperately would I?
In Unix based systems, the scripts usually start with the hash-bang line (#! ....) that says what program should be used as the interpeter of this executed file.  Then you set the "executable" flag to the script file via chmod command.  From now on, you can just type the name of the script and the OS just starts the file as the executable one.  Then the program loader checks the first two bytes (a.k.a. magic bytes, or simply magic) and decides how to continue with loading.  When #! is found, then it interprets the string to the end of the line as the name of the interpreter, launches the interpreter and passes the script file as the argument.

Windows do not use that #! information.  In Windows, you cannot make any file executable via setting something similar to the executable flag.  Instead, Windows work with association of a file extension and the application that processes the file.  It could be a kind of equivalent for scripts.  However, it can be used for the document-file extensions -- you doubleclick the FairyTale.doc, and the MS Word is launched for the file.

When you tried to launch the "c:\python25\check_esxi_hardware.py" from command line, the association of *.py with the installed Python was used.  However, the same does not work from the batch file which is already consumed by its interpreter (probably the cmd.exe).  This is the reason why you should do it as HonorGod suggested. Probably python.exe or just python would be enough if you are sure that it is the first python.exe that can be found via PATH.

Or you can try the 'start "c:\python25\check_esxi_hardware.py" ...' in your batch file, but I am not sure here.  Using plain 'python "c:\python25\check_esxi_hardware.py" ...' is less obscure.
If you really do not need Python 2.5 for being compatible with you Linux Python, then you should better choose the last Python 2.7.
HonorGodSoftware Engineer
> ... (python.exe) is in in the "c:\python25" folder so I would not need to call it seperately would I?

  Yes, that is the program that needs to execute.  It, in turn, will load, parse, and execute your script.

So, you need to have your .bat file either do something like this:

"c:\python25\python.exe" "c:\python25\check_esxi_hardware.py" –H esxhost –U root –P mypasswd –V hp

Open in new window

Or perhaps something like this:
cd c:\python25
python check_esxi_hardware.py –H esxhost –U root –P mypasswd –V hp

Open in new window


Thanks a lot for your advice and help folks. The script is working now.
HonorGodSoftware Engineer

Thanks for the assist, and the points.

Good luck & have a great day.