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

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?

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...

Related

Open terminal, run python script and keep it open for results?

How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).

Python 2.5 to Python 2.7, script from Batch file won't stop

I have a BAT file that makes something like
echo "Start"
python myScript.py <arguments>
del <file>
echo "Finished"
When executing it, it never reaches the line where the del command is located. I don't know if there is needs of launching the python interpreter with an additional option, but that happens since I upgraded from Python 2.5 to Python 2.7.6.
Well,
I found that configuring the threading module in my python script for lauching new threads as daemons...
threading.Thread.daemon = True
My batch file works as it used to!
Thanks a lot for your suggestions!

Shell script called using Python subprocess call() takes no effect

I am having problems executing a shell script using subprocess.call() and I made this test in Python console to try to figure out which is the problem:
import subprocess
subprocess.call(["touch", "/tmp/out.txt"])
This works and creates the file in tmp folder. However, none of these two calls work:
subprocess.call(["sh", "/tmp/test.sh"])
subprocess.call(["/tmp/test.sh"])
/tmp/test.sh:
#!/bin/bash
touch out.txt
exit 0
Basically, executing an script from subprocess.call() is not producing any output. I gave full permissions to files and folders to avoid any problem. It seems the problem may be related to the user executing the script, but it is the same user as in the first case, which is working.
Any idea what the problem could be?
BTW, I am using Ubuntu 12.04 LTS, Python 2.7.4.
Thanks in advance.
Your script is executed correctly. The problem is with your bash script.
You have to understand that this script inherits working directory of the parent script. Therefore out.txt will be created in the directory where you run the python script which makes use of subprocess.call.

Is it possible to build a py2exe console app with the "-i" python argument?

I'd like to make a py2exe distribution of a python program that invokes python with the -i command line option. Does anyone know if that's possible? Also, for this to work, do I need to do something about sys.exit()?
You can start the interactive Python shell by invoking magic in your script^W^W^W^W manually calling the REPL. This would be roughly equivalent to the -i flag (except you can start the interpreter at ANY time, not just at the end of your script.)
The magic looks like this (courtesy of Jason R. Coombs):
def interact():
import code
code.InteractiveConsole(locals=globals()).interact()

Python print in embedded python wont display anything to cmd

I have an application that launches a python script with parameters.
When the script runs i print some info on the cmd.
The funny thing is that i can't see anything from the print function on the cmd.
If i redirect sys.stdout to a file i get what i want to print which is strange.
I'm using python 2.6.4.
Anyone encountered something similar?
Are you using cmd.exe on windows? If so, there was or still is known problems trying to print to it using python. I believe the issue is a UTF-8 encoding problem. I have heard the following batch file will fix this issue (never tested).
set PYTHONIOENCODING=UTF-8
cmd /u /k chcp 65001
set PYTHONIOENCODING=
exit
Also its worth checking that your output is actually being flushed, try adding the following:
import sys
sys.stdout.flush()

Categories