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()
Related
from os.path import join
string=" Congratulations, you are about to embark upon one of life’s "
path=r"C:\Users\Nord.Kind\Desktop"
file="test.txt"
quit()
# This should not execute, but it does!!
with open(join(path,file),"w")as wfile:
wfile.write(string)
wfile.close()
In the above code example the code still executes the write in file command, even though its after a quit.
The same behavious occures when I use exit.
I am using Spyder 3.6
Also the kernel restarts each time I use exit or quit.
Any help?
(Spyder maintainer here) Your question contains this comment:
Also the kernel restarts each time I use exit or quit.
That's the behavior of the IPython kernel we use as a backend to execute users code. Those commands kill the kernel and that forces a kernel restart to maintain its associated console active. I'm afraid there's nothing you can do about it.
Note: The same happens in the Jupyter notebook.
One way is to use sys.exit() in lieu of quit()
import sys
... # code that executes
sys.exit()
... # this code won't execute
However, as noted by #AranFey in the comments, your code will throw an error if it attempts to execute the last part where the variable read is not defined.
You can use SystemExit:
# Code that will run
raise SystemExit
# Code that will not run
sys.exit() also raises this error but this doesn't require importing sys.
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!
I've written a python script that I am attaching to a watchpoint in LLDB, such as:
def wpCallback(frame, wp, internal_dict):
...
and I am attaching the callback with:
watchpoint command add -F commands.wpCallback watchpointID
I would like execution of the program to immediately resume after wpCallback is finished. Currently, execution halts as the watchpoint normally would. Is it possible to silently continue after the function is done? Based on this answer it seems like you can do something like this in GDB:
break foo if x>0
commands
silent
do something...
cont
end
You should be able to call SBProcess.Continue() on your process in your watchpoint callback. I.e. if you called the first argument to your callback frame do:
frame.thread.process.Continue()
That works for breakpoints, but seems to be broken for watchpoints in current TOT lldb. It looks like it disables the watchpoint. That's:
https://llvm.org/bugs/show_bug.cgi?id=28055
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.
so after the upgrade of gevent to 1.1rc4 (from 1.0.2) while running through pycharm, I can't get greenlets to join properly... take this code for example:
from gevent import monkey, Greenlet, hub
import gevent
hub.Hub.resolver_class = ['gevent.resolver_ares.Resolver']
monkey.patch_all()
def sleepy(time):
gevent.sleep(time)
print "done like a good script"
if __name__ == '__main__':
g = gevent.spawn(sleepy,10)
g.start()
g.join()
print "if this is the only log line, then join didn't work"
will output:
"if this is the only log line, then join didn't work"
from the IDE, it executes normally using the same interpreter from the CLI
I have followed the code in the cli and gui and there is a difference in behavior greenlet.join() caused by a change in behavior in hub.switch() :
def switch(self):
switch_out = getattr(getcurrent(), 'switch_out', None)
if switch_out is not None:
switch_out()
return greenlet.switch(self)
where that last line, will return immediately, before the greenlet is executed...
the pycharm debugger wont let me step into that code...
any help would be great... coroutine flow control is hard enough when it works...
Pycharm does some magic (not always nicely) with stdin.
Try to flush your stdin right after each print statements by doing this:
import sys
sys.stdin.flush()
Also add time.sleep(1) at the very end.
It might be that your script exit before the print statement was outputed. I don't know if that will work, but definitely worse a try.
If it does not work, try outputting to a file and see if that works properly.
I opened a bug at jetbrains, it was marked as a duplicate of: https://youtrack.jetbrains.com/issue/PY-14992
I haven't looked at all of my debug issues with gevent, but this simple case was fixed with the 2016.1 release that went out 3/23/2016