I have a Python script that loads a CSV file of data and then runs functions on each line using a for loop. It all works great apart from consistently at item number 247 it exits. No error message, I just get returned to a command prompt.
I have tried entering debug statement to narrow down what is happening bu I am still no clearer as there is no error message.
Is there a way to run Windows Python is some sort of verbose mode so I can watch it running and see why my loop is stopping?
You could use the pdb module, with a selective if condition to invoke it for row containing item 247 only. Something like below, which will enter the interactive debug mode for the case when its not working:
for line in csvfile:
if row_item == 247:
import pdb; pdb.set_trace()
# regular processing here
From there, you can step into the function to understand what is not working.
Related
If I run the toy script below as shown:
import sys
sys.path.append('/usr/share/anki')
import aqt
app = aqt._run(argv=['/usr/bin/anki', '--profile=test'], exec=False)
# breakpoint()
print(repr(aqt.mw.col))
aqt.mw.cleanupAndExit()
...I get the following output, which is not right:
$ python3 /tmp/ankiq.py
None
If I uncomment the commented statement, however, and re-run the modified script, I get the correct output (eventually):
$ python3 /tmp/ankiq.py
> /tmp/ankiq.py(8)<module>()
-> print(repr(aqt.mw.col))
(Pdb) c
<anki.collection._Collection object at 0x7f32ec1417b8>
I would like to avoid the need for the breakpoint() statement (and for having to hit c whenever I want to run such code).
My guess is that, when the breakpoint() statement is commented out, the print statement happens before aqt.mw has been fully initialized.
(I tried replacing the breakpoint() statement with time.sleep(1), but when I run the script with this modification, it hangs before ever printing any output.)
Q: How can I modify the toy script above so that, by the time the print statement executes, aqt.mw.col has its correct value?
It seems that calling aqt._run(*args, exec=False) returns a QApplication object - but without starting its event-loop. To manually process pending events, you could therefore try calling app.processEvents().
From the comments, it appears the exact solution is as follows:
while aqt.mw.col is None:
app.processEvents()
I have a python code that runs from the command line (Windows) and crashes on a MemoryError.
While I work on solving the error I want the code to re-run, with a flag, for example python c:\python_code.py --rerun, until I get a successful exit code.
Is this possible in Windows?
Also, it is key that the code restart, otherwise it is my understanding that the memory is not cleared.
Thank you
you can wrap your code around:
if flag==rerun:
while True:
try:
os.system('python python_code.py')
break
except MemoryError:
pass
Alright, I so here are the facts. I have 2 python scripts and I want Script1 to trigger Script2. I have tried the following ways to do this:
from subprocess import call
call(["python3", "script2.py"])
The dreaded exec call:
exec(open("script2.py").read())
And finally:
os.system("script2.py 1")
So just to make sure I am giving you all the info needed. I want to run script1 first then once it is finished processing I want script1 to trigger script2. Currently no matter what I have tried, I get stuck in a loop where script one, just simply keeps running over and over again.
Any ideas?
Here is the actual code for script1:
import os
"""This looks like it is unnecessary but I can't include its context
in this post. Just know it has an actual purpose."""
input_file = "gs://link_to_audio_file.m4a"
audio = input_file
output_format = os.path.basename(input_file).replace("m4a", "flac")
os.system('ffmpeg -i %s -ar 16000 -ac 1 %s' % (audio,output_format))
os.system("python3 script2.py")
Make sure the first script runs cleanly by itself by commenting out the call to the second script. If it still seems to run forever there's an issue other than trying to call a second script. If you have a IDE, you can step through the code to discover where it hangs. If you're not using an IDE, place print statements in the script so you can see the execution path. Do you possibly have a cyclic call? So the first python script is calling the second and the second python script is in turn calling the first?
When using os.system, I believe you'd need to include python as in
os.system("python script2.py 1")
I can't tell why you're in a loop without seeing the scripts.
I have finally solved this issue! I was actually using an import statement in the second script that was trying to import a variable from the first script, but instead it was importing the entire script, causing it to run in an endless loop. Just like LAS had suggested, nicely done! Thank you all for all your help on this!
In interactive mode, is there a way to signal the end of a statement (such as a class definition) and return to the prompt in order to then instantiate objects?
I've gone through simple exercises - calculations, ifs, loops, while statements. And the interpreter "gets" that the statement is complete.
It seems a simple question, but I've had no luck searching either in stackoverflow or the web generally.
(More generally, are there limitations re: what you can do in interactive mode vs via a script. Or should one be able, in theory, to experiment with all aspects of the language?) Thanks.
You can type anything in the IDLE console. Function and class definitions, like loops, are multi-line statements. A blank line at the IDLE prompt (also at the regular commandline python prompt) terminates a statement.
The main differences between scripts and the python prompt are:
a) In a script, a function or class definition, a loop, or even the inside of a pair of parentheses can contain empty lines; on the IDLE console, a blank line will terminate and execute a statement. E.g., You can't successfully type the following at the IDLE prompt:
def something():
x =0
return x
b) The IDLE console will print the value of any expression evaluated at the command prompt. In a script, you need to use print or the value will disappear.
>>> 2 + 2
4
Note for completeness: A blank line will not terminate a syntactically incomplete statement (e.g., unmatched parentheses). But never mind that.
I'm writing a parser in Python that outputs a bunch of database rows to standard out. In order for the DB to process them properly, each row needs to be fully printed to the console. I'm trying to prevent interrupts from making the print command stop halfway through printing a line.
I tried the solution that recommended using a signal handler override, but this still doesn't prevent the row from being partially printed when the program is interrupted. (I think the WRITE system call is cancelled to handle the interrupt).
I thought that the problem was solved by issue 10956 but I upgraded to Python 2.7.5 and the problem still happens.
You can see for yourself by running this example:
# Writer
import signal
interrupted = False
def signal_handler(signal, frame):
global interrupted
iterrupted = True
signal.signal(signal.SIGINT, signal_handler)
while True:
if interrupted:
break
print '0123456789'
In a terminal:
$ mkfifo --mode=0666 pipe
$ python writer.py > pipe
In another terminal:
$ cat pipe
Then Ctrl+C the first terminal. Some of the time the second terminal will end with an incomplete sequence of characters.
Is there any way of ensuring that full lines are written?
This seems less like an interrupt problem per se then a buffering issue. If I make a small change to your code, I don't get the partial lines.
# Writer
import sys
while True:
print '0123456789'
sys.stdout.flush()
It sounds like you don't really want to catch a signal but rather block it temporarily. This is supported by some *nix flavours. However Python explicitly does not support this.
You can write a C wrapper for sigmasks or look for a library. However if you are looking for a portable solution...