[visionegg] Re: OSX Python IDE issues

Alex Holcombe wrote:

On this topic of OSX python idiosyncrasies,
I have some questions about using the interactive editor, PythonIDE. One of the
things that attracted me to try visionegg was the prospect of significantly
shorter debugging times and debug-execution cycles thanks to Python being an
interpreted rather than compiled language. In the case of MATLAB, having the
interpreted language makes programming quite speedy because one can test small
sets of lines of code by copying them directly from the program file to the
command line and debugging them in isolation- e.g. allowing one to easily set
individual variables manually and checking the result.


I was disappointed to find that I was not able to do this with Python programs
using PythonIDE (also I didn't find any alternative IDEs on the web that might
take care of this). There appear to be three reasons for this.


1. when I copy (using command-C or the menu) text from a Python script and paste
it in on the command line, only the first line of the text is actually executed,
even though manually entering the same lines one by one with carriage returns
between them does execute the code perfectly. Why the problem with pasting
occurs I don't know- does anyone know?


2.Even if the above did work, trying to execute a few lines of code out of a
Python script should typically not work, if I understand Python syntax
correctly.  Because to Python indentation whitespace is meaningful, one cannot
e.g. copy the contents out of a nested if-then statement and expect it to
execute, because when you paste the text it will have that level of indentation
still rather than having no indentation as it should.  Am I right about this and
is there a way around it?

3. One nice thing about MATLAB is I can execute a script and then query the
values of all the variables that the script affects from the command line.  By
just typing the variable name, I can see its value.  In contrast, it appears
that in Python variables have more restricted scope such that the command line
can't "see" the variables executed in a Python script.  Is there some way to
make the variables in a Python script global so that I can play with them on the
command line?

Without solutions or workarounds to the above three issues, much of the everyday
efficiency advantage of using an interpreted language seems to me to be lost. So despite the beauty of Python, for my new lab I might end up using MATLAB and
Psychophysics toolbox (presuming they someday get that to work with OSX!).


Thanks in advance for any corrections comments etc.,
Alex Holcombe



Dear Alex,


I have a suggestion for you: try IPython's "magic" run -i function. It will address most of your concerns, albeit though an alternative workflow than the Matlab IDE. "run -i foo.py" (or simply "foo") will run the script within IPython's interactive namespace. For me, this approach really solves the problems you raise above. However, here's some background to help you determine if this is worth pursuing for you:

My "IDE" is xemacs and the command line. In general I write standalone scripts. I do use interactive sessions for exploratory data analysis (typically in IPython/matplotlib), simple arithmetic, and (like you suggest) trying simple lines of code.

I must admit that I've never spent much time trying IDEs. For me, the more I use XEmacs, the better, because it works exactly the same on every platform I use and handles most of the text editing I need (except writing emails and editing .doc files). Unfortunately, most IDEs I've tried aren't so cross-platform. And another problem for me is that when I edit Python code, my hands involuntarily make lots of emacs keystroke commands, which is really counter-productive if I'm not in some form of emacs! :)

Python's namespace management is a good thing, but probably means cutting-and-pasting a few lines of code to the interpreter has more difficulties than simply syntax errors related to parsing. (If my rapidly aging knowledge of Matlab is still correct, everything is a global variable in Matlab with the exception of variables within functions.) Again, I find IPython's run -i command to be a good solution.

Because I don't use an IDE, my debugging process generally involves sprinkling print statements throughout the code. This seems to work pretty well for me. The initialization time of most code I run is trivial. The primary exception is data mining, but then I start using IPython and it's "magic" run -i function. In that case I often use this trick:

try:
   variable_that_takes_a_long_time_to_instantiate
except NameError:
   # This is the first time we're run this code
   variable_that_takes_a_long_time_to_instantiate = long_running_function()

Or, if I'm simply refining how a plot will look, I save the raw plot points to a .csv file (with Python 2.3's new .csv file support) and then tweak plotting parameters within a simple script which loads this file. This also has the advantage that you can open the .csv file in a spreadsheet or other program.

The other exception to the trivial code initialization time is unfortunately the startup time for the Vision Egg on Mac OS X. This isn't the Vision Egg's fault, per se (AFAIK), but my OS X development machine is a 3 year old 400 MHz G4 laptop and I'm pretty sure there are some highly non-optimized code paths in the dependencies (e.g. Tkinter). So, I must admit that I do most of my Vision Egg development in linux. Maybe one day I'll get a dual G5, but for now I keep dreaming...

Finally, I would suggest that you forward your email (and my response, if you'd like) to comp.lang.python -- that has to be one of the most useful and friendly discussion groups on the net.

Cheers!
Andrew
======================================
The Vision Egg mailing list
Archives: http://www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html

Other related posts: