Why GASP doesn't work when I run it from IDLE? - python

I made this script:
from gasp import *
begin_graphics()
Circle((200, 200), 60)
Line((100, 400), (580, 200))
Box((400, 350), 120, 100)
update_when('key_pressed')
end_graphics()
When I start it from terminal, it works perfectly. When I run it from IDLE, it doesn't work, I get no answer (shell prompt (>>>) disappears but nothing happens).

In general, you can't run GUI apps in the embedded Python interpreter in IDLE, unless the library you're using is designed to integrate with IDLE. Or, worse, it may work on one machine and not on another. I'll explain why below, but first just take that on faith.
As far as I can tell, gasp's documentation doesn't address this, but similar libraries either warn you that they may not work in IDLE (easygui, early versions of graphics, etc.) or come with special instructions for how to use them in IDLE (e.g., later versions of graphics).
Now, maybe gasp should be designed to integrate with IDLE, given that it's specifically designed for novices, and many of those novices will be using the IDE that comes built in with Python. Or maybe not. But, even if that should be true, that's something for gasp to deal with. File a bug or feature request, but you'll need some way to keep working until someone gets around to writing the code.
The simplest solution here is to use a different IDE, one that runs its interactive Python interpreter in a completely separate process, exactly the same as you get when running it yourself in the terminal. There are lots of good options out there that are at least free (-as-in-beer) for non-commercial use (PyCharm, Komodo, Eclipse PyDev, emacs with your favorite collection of packages, etc.). Although Stack Overflow is not a good place for advice on picking the best one for you (if googling isn't sufficient, try asking on a mailing list or forum), almost any of them will work.
Another option: instead of using an interpreter built into an IDE, you might want to to consider running an enhanced interpreter environment (like ipython-gtk or emacs with a smaller set of packages) alongside your IDE. Of course they'll no longer be tightly integrated (the "I" in "IDE"), but in my experience, even working in an environment where the whole team uses PyCharm or PyDev, I still end up doing most of my interactive testing in ipython; you may find you prefer that as well. Or you may not, but give it a try and see.
So, why is there a problem in the first place?
First, if you don't understand what an "event loop" or "runloop" or "mainloop" is, please read either Why your GUI app freezes or the Wikipedia page or some other introduction to the idea.
Normally, when you run the interactive Python interpreter (e.g., by typing python at the bash or C: prompt in your terminal), it runs in its own process. So, it can start up a runloop and never return (until you quit), and the terminal won't get in your way.
But when you run the interactive Python interpreter inside IDLE, it's actually running in the same process as IDLE, which has its own runloop. If you start up a runloop and never return, IDLE's runloop doesn't get to run. That means it doesn't get to respond to events from the OS, like "refresh your window" or "prepare for a new window to open", so from the user's (your) point of view, IDLE and your app are both just frozen.
One way to get around this is in your code is to spawn another thread for your runloop, instead of taking over the main thread. (This doesn't work with all GUI libraries, but it works with some. This is how graphics solved the problem.) Another way is to spawn a whole new child process to run your GUI. (This works with all GUI libraries, but it's a lot more work—now you have to deal with inter-process communication. One of the novice-friendly matplotlib wrappers does this.) Finally, you can integrate your runloop with IDLE's Tkinter runloop by having one drive the other. (Not all GUI libraries can be driven this way, but Tkinter's can, and IDLE can be monkeypatched to work this way; graphics used to do this.) But none of these are even remotely simple. And they're probably things that gasp itself should be doing, not your code.

Related

Python & Windows: Spawning a process, then dying without showing the cmd prompt

I've got a problem with a python script which is responsible for syncing changes from a VCS (among other things) which may include changes to itself / libraries it depends on. In such cases, I would like to be able to detect if the sync touched anything I depend on, and restart if that was the case.
On POSIX platforms this is easy. exec(), done. On Windows it's incredibly annoying. I can Popen or exec*() and then die, but the problem is my user will see the cmd prompt show a new line, which messes up the flow of output, not to mention completely breaks any future input on stdin, allows them to run commands while my script is still running, which is undesirable.
I've searched high and low without finding anything approaching an answer to this (and yes, I know there are alternative ways to accomplish the same goal, but it'd be nice to have this work). Seemingly the problem is telling the console that the new process is now the "owner" of the console, but I can't find a way to do that. I tried calling https://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx through ctypes, and that didn't work either.
Any help much appreciated. Thanks!

In Python, how do I debug with an interactive command line (and visual breakpoints?)

I've recently moved from Matlab to Python. Python is a much better language (from the point of view of a computer scientist), but Python IDEs all seem to lack one important thing:
A proper interactive debugger.
I'm looking for:
The ability to set breakpoints graphically by clicking next to a line of code in the editor.
The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc.
When an error occurs, the debugger should automatically open an interactive console at the error line.
Once done with the interactive console, you can resume normal execution.
Matlab has all these features and they work incredibly well, but I can't find them anywhere in Python tools.
I've tried:
PyCharm: the interactive console is clunky, often fails to appear, and crashes all the time (I've tried several different versions and OSs).
IPython: can't set breakpoints -Launching a Python console programatically: you have to stop your code, insert an extra line of code, and run again from the beginning to do this. Plus, you can't access functions already imported without re-importing them.
Being able to debug and fix problems THE FIRST TIME THEY APPEAR is very important to me, as I work in programs that often take dozens of minutes to re-run (computational neuroscience).
CONCLUSION: there is NO way to do all of these in Python at the moment. Let us hope that PyLab development accelerates.
At the top of your code, write
import pdb
And within your code, use the following statement wherever you want to debug.
pdb.set_trace()
You will have an interactive shell thus, whenever the set_trace() statement is met.
You can then use step(s), next(n), continue(c) and so on to check the execution flow, and print values of variables like print var
For more details on pdb, refer here
There are many Python IDEs. That was a topic here: What IDE to use for Python?
"The ability to set breakpoints graphically by clicking next to a line of code in the editor."
PyDev has this. Double-click in the gray margin bar.
"The ability to run ANY CODE while stopped in the debugger, including calling functions from my code, showing new windows, playing audio, etc."
PyDev has this. It's not the only one. PyScripter's stated features seem to include this.
"When an error occurs, the debugger should automatically open an interactive console at the error line."
PyDev does this. (I think. Or at worst do you need to double-click on the console message that states the error's location in the code?)
"Once done with the interactive console, you can resume normal execution."
PyDev has this. It's called "resume". It's what the green "play" triangle in a toolbar does. Some other software calls this feature "continue".
I've been searching for the same, but unfortunately Python IDEs are not as well-featured as Matlab's at this point. For scientific programming, you will also want graphics/plotting to run in an entirely different thread, so IPython integration is essential. As far as I can tell, the Matlab IDE feature to change the workspace from the debugger, which then affects code running subsequently, is quite unique. Each of the features exist in some IDE, but none exist in all:
Spyder has good integration with scientific tools, but its debugging is limited to the built-in pdb, which lacks the requirement to execute any code and have this code affect the namespace after continuing.
PyDev and PyCharm, and quite a few others, have decent debugging features, but I don't think it has good integration with scientific tools. That means, if you plot, you lose access to your prompt. Not good.
As far as I've experienced, the closest comes Wing IDE. It is a propietry product, but if you're making the transition from Matlab 89$/year for non-commercial-use should be acceptable (you can evaluate it first). But for me, I've ultimately settled to alter my workflow, and not using any sophisticated IDE at all. When I looked was some years ago, so perhaps the situation has improved.
You might also be interested in this article from April 2013, Evaluating IDEs for Scientific Python. It doesn't really reach a conclusion either.
Seeing as you are comming from Matlab, I would suggest you give a look at
Python(x,y)
The page decribes it as follows:
Python(x,y) is a free scientific and engineering development software for numerical computations, data analysis and data visualization based on Python programming language, Qt graphical user interfaces and Spyder interactive scientific development environment.
It will not cater to all your wishes, but it certainly made me feel comfortable when I started out with Python, coming from Matlab.
Hope it helps
You can do all this in the iPython Notebook. Use the magic command %pdb to stop on error.
If using the command line,
alias ipythondebug='ipython --InteractiveShell.pdb true'
in your ~/.profile will give you debug on error like Matlab. This of course requires ipython installed.
Not sure about the resuming part.
You can also edit the ipython config file if you want the debug on error to be permanent. See
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pdb

Subprocess crashes unexpectedly from PyDev, works fine from double click in windows explorer

I've spent quite a bit of time trying to figure this out. I'm trying to invoke this line to run abaqus (an FEA program):
popen = subprocess.Popen(callCommand, cwd=workDir, creationflags=subprocess.CREATE_NEW_CONSOLE)
popen.wait()
When double clicking on the .py file everthing works fine. However on running it from Eclipse, Abaqus crashes:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Then later I also get "SMAPython.exe has stopped working".
I've played around with admin privilege settings but to no avail. Don't have the rep to tag it with Abaqus.
The solution (which I've come accross after writting a draft for the question) was found here:
http://sourceforge.net/p/pydev/discussion/293649/thread/94a76ecb/
Basically, PyDev adds some environment variables that don't play well with Abaqus, so to turn them off the following code can be used:
import os
try:
os.environ.pop('PYTHONIOENCODING')
except KeyError:
pass
# now call abaqus...
Hopefully this is of use to someone, I've spent almost two days fixing this. It is a bit of a niche use of PyDev (I'm not a programmer, I'm a Civil Engineer) but I think it is much more powerful to have Eclipse take care of all the source files. Abaqus CAE files are all binary and proprietary so source control and custom edits are a pain otherwise.
I guess in any case the solution is to trace the problem by taking bits of it off and checking what works and what the differences are.

Easiest way to pop up interactive Python console?

My application has a Python interpreter embedded in it. However there's currently not any way to inspect anything about Python directly. I'd like to be able to pop up an interactive shell at various points to inspect what's going on in Python.
I've found several similar questions which pointed me to code.InteractiveConsole and IPython. However neither of those seem to do anything when I call them - the call appears to complete okay, but nothing visible happens, presumably because my Python instance isn't outputting anywhere I can see.
It was pretty optimistic to think that they'd just magically pop up a new window for me, but it would be nice if I could get that to happen. So, does anybody know if there is an easy way to achieve this (or something similar), or do I have to roll my own dialog and pass the input to Python and display output myself?
Thanks :)
What have you tried with IPython? Is it the snippets from the documentation:
Embedding IPython (IPython docs)
How about some of the code samples from elsewhere:
Embedding IPython in GUI apps is trivial
Embedding IPython in a PyGTK application
I know I fooled around with this a while back and the samples seemed to work, but then I never got a chance to go any further for other reasons.
I know this isn't what you're asking, but when I've wanted to debug compiled Python (using Py2Exe), I've been very pleased to realize that I can add breakpoints to the exe and it will actually stop there when I start the executable from a console window. Simply add:
import pdb
pdb.set_trace()
where you want your code to stop, and you'll have yourself an interactive debugging session with a compiled executable. Python is awesome that way :)

Python Debugging: code editing on the fly

I am new to python and haven't been able to find out whether this is possible or not.
I am using the PyDev plugin under Eclipse, and basically all I want to find out is, is it possible to edit code whilst you're sitting at a breakpoint? I.e. Edit code whilst you're debugging.
It allows me to do this at present, but it seems to still be executing the line of code that previously existed before I made changes.
Also, are you able to drag program execution back like you can in VBA and C# for example?
If either of these are possible, how can I enable them?
PyDev supports this to some extend since version 1.4.8, see the change notes and the corresponding blog entry.
When you start a Python program, it will be compiled into bytecode (and possibly saved as .pyc file). That means you can change the source but since you don't "open" the source again, the change won't be picked up.
There are systems like TurboGears (a web framework) which detect these changes and restart themselves but that's probably going to confuse the debugger.
Going back in time also isn't possible currently since the bytecode interpreter would need support for this.
You can run arbitrary commands in the console during the breakpoint. For my needs, this typically achieves the same purpose as coding live, although I do wish it were as elegant as simply using the editor.

Categories

Resources