How to capture stdout of a Python script executed with pythonw? - python

When a script is executed with pythonw it will not open a console.
Is there a way to capture the stdout of such a script by keeping the usage of pythonw?
Note, I am looking for a solution that does not require the modification of the script (I know that I can use logging)
Update: pythonw script.py >somefile seems to work. How can I redirect it to console?

It was obvious: pythonw script.py|more

If you can change how you invoke it (as you do in the update), why don't you just run it with python instead of pythonw?
python script.py

Related

How to block terminal/console outputs in python

I'm using Python to execute some bash commands. The problem is that the terminal outputs from these bash scripts are spamming my terminal. Is there any way to block the output messages from these scripts? I have tried the step in this answer. But is only blocking the print calls I make, and it is not blocking the console outputs from the bash commands.
Can anyone suggest any better solution?
In Bash you can simply use:
$ eclipse &>/dev/null
This catches both stdin and stderr to the redirect point (in bash).
(here eclipse is my command like)

Execute python code from within a shell script

I want to have some python code run within a shell script. I don't want to rely on an external file to be ran. Is there any way to do that?
I did a ton of googling, but there aren't any clear answers. This code is what I find... But it relies on the external python script to be ran. I want it all within one file.
python python_script.py
You can use a so-called "here document":
#!/usr/bin/env bash
echo "hello from bash"
python3 - <<'EOF'
print("hello from Python 3")
EOF
The single quotes around the first EOF prevent the usual expansions and command substitions in a shell script.
If you want those to happen, simply remove them.
If you mean within a BASH shell script without executing any external dependencies, I am afraid you're out of luck, since BASH only interprets its own scripting language.
Your question is somewhat like asking "Can I run a Java .class file without the JVM"? Obviously, you will always have the external dependency of the JRE/JVM. This is the same case, you depend on the external Python compiler and interpreter.
Optionally, you have the option of including the python script inline, but it would still require the python executable.
This works:
python -c 'print("Hi")'
Or this with BASH redirection:
python <<< 'print("Hi")'

"script command" and logging in python

I have a python app that has lots of outputs on the screen which can be used for debugging. out of all the logging techniques, "script" command works well for me because I can see the output on the screen as well as logging it. I want to include that at the beginning of my python app to run automatically and log everything, when I do, however, the python program doesn't run. as soon as I type exit at the terminal (which stops script logging) the app starts working. The command I'm using is:
command="script /tmp/appdebug/debug.txt"
os.system(command)
I have also tried script -q but the same issue is there. Would appreciate any help.
Cheers
Well, I did find the answer for anyone who is interested:
https://stackoverflow.com/questions/15507602/logging-all-bash-in-and-out-with-script-command
and
Bash script: Using "script" command from a bash script for logging a session
I will keep this question as others might have the same issue and finding those answers wasn't exactly easy :)
Cheers
Try to use subprocess, like so:
from subprocess import Popen, PIPE
p = Popen(['script', '/tmp/appdebug/debug.txt'], stderr=PIPE, stdout=PIPE)
stdout, stderr = p.communicate()
script is a wrapper for a session of interactions. Even if it appears to terminate quickly after being started in a shell, this is not so; instead it starts a new shell in which you can interact so that everything is logged to a file.
What does this mean for you?
Your approach of using script cannot work. You start script using os.system which will wait for script to terminate before the next Python statement is executed. script's work will only happen before it terminates (i. e. during the uninteresting waiting period of your Python program).
I propose to use script -c yourprog.py yourprog.log instead. This will execute and wrap the yourprog.py and the session will be stored in yourprog.log.

& after bash command

I'm not sure what the & after the command in this bash script is doing.
python alt_pg ${args} &
Also the original version of the script that I'm modifying does not use 'python' at the start of the command is that something to do with the '&'?
& at the end of the line runs python alt_pg ${args} in the "background" under your linux shell; however, the script is still associated with the shell. Therefore, if the shell stops, so does the script.
Side note: You can disassociate the script from your shell by using nohup python alt_pg ${args} &. If you spawn the script like this, the script persists after logging out of the shell.
No, they're two separate things.
Running python alt_pg ... means python will be looked up in $PATH, and alt_pg ... will be passed as arguments to python. Python then looks for a file named alt_pg. Running alt_pg ... means alt_pg will be looked up in $PATH. The latter may cause python to run anyway, depending on what alt_pg does.
Adding a & after the command means the command runs in the background, and the shell can continue with commands that follow even when alt_pg is still running.
The ampersand runs the process in a forked/background process.

Inside python code, how do I run a .sh script?

Will it continue the code after it's run? Or will it stop at that line until the script is done?
Using subprocess.call is the easiest way. It will not return until the executed program has terminated. Have a look at the other methods of the subprocess module if you need different behaviour.
import os
os.system('./script.sh')
python script won't stop until sh is finished
You can use os.system or subprocess.Popen or subprocess.call but when using subprocess methods make sure you use shell=True. And executing it via system call in all these methods is blocking. The python script will complete and then go the next step.

Categories