Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows - running .py directly vs running python blah.py behaves differently

I have a python script that uses subprocess:

import subprocess
print "Running stuff"
subprocess.check_call(["do_stuff.bat"])
print "Stuff run"

If this was named blah.py, and I run (from a command prompt):

python blah.py

I will get the output from do_stuff.bat (or whatever I run).

If this is run as:

blah.py

Then I do not get output from do_stuff.bat, only the print statements.

So far seen on windows Server 2003. Python version 2.5.2 (stuck there for various reasons). Looking at the associated file type action I see:

Python.File="C:\Python25\python.exe" "%1" %*

So can anyone explain the difference?

like image 439
Danny Staple Avatar asked Aug 18 '11 09:08

Danny Staple


People also ask

What is the difference between py and Python in CMD?

The python command is the standard installed alias for python2 or python3. the py command is a non-standard command line tool which seem to invoke Python3 by default and gives command line access to python constructs . The py command uses it's own method for identifying which python version to use.

Can you run Python scripts on Windows?

On recent versions of Windows, it is possible to run Python scripts by simply entering the name of the file containing the code at the command prompt: C:\devspace> hello.py Hello World!

Why does py work but not Python?

When you installed Python, you didn't check the box to add it to your PATH , which is why it isn't there. In general, it's best to use the Windows Python Launcher, py.exe anyway, so this is no big deal. Just use py for launching consistently, and stuff will just work.


1 Answers

I had common problem using threads, but all of my code was in python. Threads can not write to standard output using print. Just main thread could do that. I used somethnig like this

import sys
sys.stdout.write("this was printed by thread")

I know that probably it wont help you with bat file...

like image 145
Jazzuell Avatar answered Sep 28 '22 12:09

Jazzuell