I know this has been discussed here before, but I haven't found a solution that will work for me. I already have a python script that I wrote, and I currently have it run at boot. What I would like to do is log all outputs, which include any print statements to the console, or any error messages that would come up. I do like the Logging module, and would prefer to use that over looking at all outputs on the console. Any suggestions?
If you manage your script using supervisor it will automatically handle all logging of stdout/stderr for you.
Additionally, it can automatically restart your script if it were to crash
Related
Can I redirect stderr when running or debugging python code in VS Code? Sometimes I don't want to see all the messages that imported modules spit out. Very often they aren't helpful. I'd like to be able to send stderr to /dev/null or another file sometimes, but not always. I can run my python program that way directly in the terminal, but I want to do this when debugging so I can see my output without it scrolling quickly by. Thank you.
In the debugger press the configuration file down arrow. It probably says current program. Press add configuration. Then towards the bottom of the json file add this:
"args": ["2>/dev/null"]
Remember to put a comma at the end of the previous line. Then run the debugger.
I have a Python script that is running as a cron job every day. I'm trying to get it to only output when necessary so that cron doesn't email me unless there is a warning or error. However, DEBUG-level output is still being emailed to me, even though I've set the logging level to WARNING.
import logging
logging.basicConfig(level=logging.WARNING)
When I run the script through IDLE this works as intended with no output. (If I omit those two lines, I get a bunch of DEBUG output). But when the script runs through the cron schedule, I'm being emailed those DEBUG lines.
My crontab is simple:
DISPLAY=:0.0
0 6 * * * python3 /home/pi/script.py
Any ideas?
I figured it out. I was setting the default logging level after importing the module that was automatically generating that debug output. Kind of obvious now.
I build my code using the nonconsole option when distributing my tools to users:
pyinstaller test.py --noconsole
Because without a console the look is simpler and more beautiful.
However, when an error occurs in the user's environment, there is no way to know the content of the error if it is a nonconsole.
I want to switch between console and non-console after building code.
I want to hide the console normally, so that the console can be displayed only when switching to error checking mode.
Isn't that possible?
I am not sure this is possible - hiding/showing the console is a compile/build option, rather than a run time one.
Have you considered using the logging module?
This will give you an easy way to log messages to a file, which you can inspect if something goes wrong.
I have the situation that I want the job done from eclipse so I use eclipse's .launch configuration. I tried to make it run python directly but got error: error 193 (%1 is not a valid Win32 app). where %1 is probably my python script.
I decided to create a simple batch script that calls this big wild python animal.
I did a lot of combinations and found this the best (batch outputs some strings, runs python, waits for it, the outputs some strings again):
start /b /wait "Python_script.py" "%1" "%2" "%3" "%4" "%5"
It worked until python itself started to run exe file.
Once again I tried a lot of combinations:
os.system([exe, arg1, arg2, ...]) and
subprocess.call(..) and subprocess.check_output(..)
-> I either didn't see the output in eclipse console, or the output was delayed or there was only python / or only exe's output in console.
finally I used subprocess.Popen(...) and it's nearly allright - the only defect is that the output from python script don't wait for exe's process to finish, and when I use subprocess.Popen(...).wait() exe passes output to console but the WHOLE output from python script is delayed until the 'exe' terminates. I want to delay only the part of pythons script output that is written after the exe is called.
how to achieve this 'partly console output delay' is the main topic
advices on python and eclipse .launch configuration will be appreciated
general advices on how does the communication between this processes(?) work will be appreciated
Thanks!
It sounds to me like you have three different processes you're trying to get to work together, you've tried a whole bunch of stuff to get it working, and the code is complex enough that you can't easily post it here. That makes it pretty hard to get a good answer (Stack Overflow works much better with focused questions), but here's the general approach I'd take:
Does your script run if you try to run Python_script.py directly from the command prompt?
If it doesn't, then look into registering the .py file type in Windows.
If it does, then maybe Eclipse launch configurations don't support or don't properly support Windows registered file types. There should be no need to mess with batch files and start; just replace Python_script.py in your launch configuration with c:\Python27\python.exe Python_script.py (or similar).
Get your script working from a command prompt - able to run, with proper Python and subprocess output, and waiting for everything to terminate.
If things work from the command prompt and still don't work from Eclipse, then post a new question with a small snippet of code showing what you're trying and a description of what's wrong. subprocess.call, subprocess.check_output, and Popen all have different uses, so it's hard to give general advice besides just referring to the documentation.
I am developing FUSE filesystem with python. The problem is that after mounting a filesystem I have no access to stdin/stdout/stderr from my fuse script. I don't see anything, even tracebacks. I am trying to launch pdb like this:
import pdb
pdb.Pdb(None, open('pdb.in', 'r'), open('pdb.out', 'w')).set_trace()
All works fine but very inconvenient. I want to make pdb.in and pdb.out as fifo files but don't know how to connect it correctly. Ideally I want to type commands and see output in one terminal, but will be happy even with two terminals (in one put commands and see output in another). Questions:
1) Is it better/other way to run pdb without stdin/stdout?
2) How can I redirect stdin to pdb.in fifo (All what I type must go to pdb.in)? How can I redirect pdb.out to stdout (I had strange errors with "cat pdb.out" but maybe I don't understand something)
Ok. Exactly what I want, has been done in http://pypi.python.org/pypi/rpdb/0.1.1 .
Before starting the python app
mkfifo pdb.in
mkfifo pdb.out
Then when pdb is called you can interact with it using these two cat commands, one running in the background
cat pdb.out & cat > pdb.in
Note the readline support does not work (i.e. up arrow)
I just ran into a similar issue in a much simpler use-case:
debug a simple Python program running from the command line that had a file piped into sys.stdin, meaning, no way to use the console for pdb.
I ended up solving it by using wdb.
Quick rundown for my use-case. In the shell, install both the wdb server and the wdb client:
pip install wdb.server wdb
Now launch the wdb server with:
wdb.server.py
Now you can navigate to localhost:1984 with your browser and see an interface listing all Python programs running. The wdb project page above has instructions on what you can do if you want to debug any of these running programs.
As for a program under your control, you can you can debug it from the start with:
wdb myscript.py --script=args < and/stdin/redirection
Or, in your code, you can do:
import wdb; wdb.set_trace()
This will pop up an interface in your browser (if local) showing the traced program.
Or you can navigate to the wdb.server.py port to see all ongoing debugging sessions on top of the list of running Python programs, which you can then use to access the specific debugging session you want.
Notice that the commands for navigating the code during the trace are different from the standard pdb ones, for example, to step into a function you use .s instead of s and to step over use .n instead of n. See the wdb README in the link above for details.