How are you using IronPython? [closed]
Asked Answered
C

12

28

I'm keen to drink some modern dynamic language koolaid, so I've believed all the stuff on Michael Foord's blog and podcasts, I've bought his book (and read some of it), and I added an embedded IPy runtime to a large existing app a year or so ago (though that was for someone else and I didn't really use it myself).

Now I need to do some fairly simple code generation stuff, where I'm going to call a few methods on a few .net objects (custom, C#-authored objects), create a few strings, write some files, etc.

The experience of trying this leaves me feeling like the little boy who thinks he's the only one who can see that The Emperor has no clothes on. If you're using IronPython, I'd really appreciate knowing how you deal with the following aspects of it:

  • Code editing - do you use the .NET framework without Intellisense?
  • Refactoring - I know a load of 'refactoring' is about working around language-related busywork, so if Python is sufficiently lightweight then we won't need that, But things like renames seem to me to be essential to iteratively developing quality code regardless of language.
  • Crippling startup time - One of the things which is supposed to be good about interpreted languages is the lack of compile time leading to fast interactive development. Unfortunately I can compile a C# application and launch it quicker than IPy can start up.
  • Interactive hacking - the IPy console/repl is supposed to be good for this, but I haven't found a good way to take the code you've interactively arrived at and persist it into a file - cut and paste from the console is fairly miserable. And the console seems to hold references to .NET assemblies you've imported, so you have to quit it and restart it if you're working on the C# stuff as well. Hacking on C# in something like LinqPad seems a much faster and easier way to try things out (and has proper Intellisense). Do you use the console?
  • Debugging - what's the story here? I know someone on the IPy team is working on a command-line hobby-project, but let's just say I'm not immediately attracted to a command line debugger. I don't really need a debugger from little Python scripts, but I would if I were to use IPy for scripting unit tests, for example.
  • Unit testing - I can see that dynamic languages could be great for this, but is there any IDE test-runner integration (like for Resharper, etc). The Foord book has a chapter about this, which I'll admit I have not yet read properly, but it does seem to involve driving a console-mode test-runner from the command prompt, which feels to be an enormous step back from using an integrated test runner like TestDriven.net or Resharper.

I really want to believe in this stuff, so I am still working on the assumption that I've missed something. I would really like to know how other people are dealing with IPy, particularly if they're doing it in a way which doesn't feel like we've just lost 15 years'-worth of tool development.

Update: (July 2010) This question's looking quite dated, now that there are both official IronPython Tools for VS and PyCharm both of which show a lot of promise and neither of which was around when I wrote it. Thanks to everyone who answered. To those of you who are happy without automatic refactoring tools, can I recommend that you never, ever, use an IDE with good automatic refactoring. After you have, you'll wonder why you thought you were ever happy with find-replace, and you'll never want to work without it again. If nothing else, the ability to instantly rename identifiers without worrying about replacing the wrong bit of text changes your whole approach to naming stuff.

Corrinecorrinne answered 6/5, 2009 at 17:0 Comment(2)
May I ask if your age on stackoverflow profile is real ?Litigable
You should never ask old people their age. No, of course it isn't. Jeff Atwood has far more important stuff to learn than my D-o-b.Corrinecorrinne
L
11
do you use the .NET framework without Intellisense?

Yes. I solely use emacs with IronPython. "M-/" is good enough for me. I occasionally switch to Reflector and MSDN when I really need information on the SDK.

Refactoring

You actually mean "Automatic Refactoring" when you said "Refactoring". I do a lot of Refactoring while coding. It could be better with some automation. But I'm fine with manual refactoring. It also force me to include more tests. I've never use any IDE with refactoring feature extensively, perhaps that's why I'm not suffering. Simple search-and-replace is fine for me.

Crippling startup time

Yes, this is my MAJOR pain, eventhough I'm working on 3GHz Quad-core with 4G Ram, it does takes 50s to run 359 test-cases. Half of that time (25s) is the startup time !! It seems IronPython perform some very heavy processing task when importing large module. I'm now planning to spit my tests into parts to mitigate this pain.

Interactive hacking - Do you use the console?

I use console occasionally, when I want to try some adhoc idea. Don't have problem with it. Most of the time I wrote my thought into unit-test, however.

Debugging - what's the story here?

If debugging means break-point and variable-inspection. I solely use print statement for that stuff. The kind of application I'm working on is about to live and keep being maintained for at least a decade. To handle defective code, I only bet on 3 things: comprehensible-code, unit-test and extensive-logging.

Unit testing

I never come close to any tool you mention. Just use unittest.TextTestRunner. Beside the horrible startup time, I do not have any other complains.

** UPDATED **

I just finish upgrading to IPy 2.6B1 with phenomenal result. The startup time reduce from 25s to 6s and my overall testing time reduce from 50s to 16s !!!

AMAZING !

Litigable answered 10/5, 2009 at 11:6 Comment(2)
+1 about emacs for python developmentLenalenard
As for the "refactoring" subject, I recently start to learn using "ropemacs", the python refactoring for emacs, and it's pretty good. Now I can do automatic refactoring without leaving emacs. :)Litigable
S
10

How about IronPython Tools for Visual Studio 2010?

IronPython tools provide a Python code editor, a file-based project system, and an interactive Python interpreter, all inside Visual Studio 2010, and all for free. http://ironpython.net/tools/

I think this add-in would cover most of your points.

Hope it helps,

Patrick

String answered 22/6, 2010 at 14:16 Comment(0)
F
7

IronPython is a great solution, but I keep going back to CPython for Windows. Why ?

In my experience CPython under Windows, Mac OS/X and *NIX are virtually the same. I can use almost all of the "batteries" in each environment. I frequently develop applications on Linux and ship to Windows servers. A couple of issues with threading aside, I've had no issues.

IronPython, on the other hand, is a Python language wrapper around the .Net Framework. I find it much nicer to use than C#, but that's about all. My CPython programs don't work without considerable reworking. The batteries for IronPython are different.

If your goal is to be productive on .Net then IPy is probably Ok. However, if you are seeking portable Python skills, then I would avoid it.

Fresco answered 21/5, 2009 at 23:42 Comment(0)
A
5

I am using PyDev to write IronPython code. I am trying to separate .NET specific code as much as possible so that I can run the (Iron)Python scripts from within PyDev. Thanks to PyDev, I can use Pylint which can be found at http://www.logilab.org/857. Recent versions of PyDev are able to configure the interpreter even per file or project basis. So, I can easily switch the interpreters from IronPython to CPython and vice versa. By this way, I can see that my code is portable between Python implementations. (Although I am not using, Jython 2.5 RC can be used in PyDev too.)

Having Python and IronPython 2.5 compatible code, I can use the refactoring in PyDev, see http://pydev.sourceforge.net/refactoring.html.

IronPython Studio would be a good choice if it supported IronPython 2.x, but sadly, it does not.

Atonsah answered 15/5, 2009 at 22:55 Comment(0)
F
4

PyDev, Wing, PyCharm and IronPython Tools for Visual Studio all support intellisense for .NET under IronPython. I have a feeling that SharpDevelop also has intellisense for IronPython.

Startup time improved a lot under IronPython 2.6 and again in 2.7. It is still considerably slower than CPython however.

Bicycle repairman and rope are two popular automated refactoring tools for Python. I've never found a need for either of them.

pdb (the Python standard library debugger) works with IronPython. PyDev, SharpDevelop and IronPython Tools for Visual Studio all support debugging for IronPython.

I've used integrated testing tools (Wing has good unittest support SharpDevelop supports unittest and unittest2 for IronPython, PyDev and PyCharm probably have good unittest integration too). I prefer running my tests from the command line. unittest2 is great for this as it removes the need to write your own test collection / running code.

I use the interactive interpreter for experimentation a great deal and occasionally as a souped up shell.

Falconer answered 28/7, 2010 at 11:5 Comment(3)
You see what I mean about startup time - it's taken you over a year to answer my question ;-) Interested to know how you take code out of the interactive console into a text file once you're done experimenting. Do you just retype it?Corrinecorrinne
I rarely reuse code from the interactive interpreter, it's just experimental code. If you're typing anything non trivial then do it in a python file! Copying and pasting short segments is bearable.Falconer
While I am trying to use "pdb" with Iron Python , I get this error: IronPython 2.7.6.3 (2.7.6.3) on .NET 4.0.30319.34209 (32-bit) Type "help", "copyright", "credits" or "license" for more information. >>> import pdb >>> pdb.set_trace() Traceback (most recent call last): ` File "<stdin>", line 1, in <module>` ` File "C:\Program Files (x86)\IronPython 2.7\Lib\pdb.py", line 1251, in set_trace' AttributeError: 'module' object has no attribute '_getframe'Nesmith
N
3

I've been using IronPython (and the DLR in general) extensively in my projects lately for various things. Some examples:

  • I made an document generator based on templates that uses py scripts to build and transform the context variables that are passed into the template renderer (Excel, Word, html->pdf)
  • As an expression evaluator for a specialized rule engine. The scripts are saved in a column in a database table and evaluated at runtime to determine which rule applies.
  • As steps in a ETL engine. Basically I have some components that can read Excel files and I use python scripts to transform the data and create NHibernate entities to populate a database.
  • Dynamic configuration scripts for C# components.
  • A calculation engine for some indicators that need to be created from a large set of data. I'm combining the Parallel extensions with precompiled scripts, performance is good so far.

Stuff like that. Once the Engine is properly configured, with paths and references all set, you can basically use a script for just about anything.

Almost all of these things are running in asp.net mvc applications, and so far so good. I discovered that the ScriptEngine and some components like ScriptScope are thread-safe so I can even share a configured Engine held inside an IoC container (ex. Windsor) between controllers and such.

As for an editor, I EditPlus most of the time. The VS 2010 python editor tends to be a bit unstable although quick editing is not a problem.

Natheless answered 20/5, 2011 at 21:40 Comment(0)
D
2

My use of IronPython has been limited to making quick prototypes for myself, so I can't say too much about it. But if you're looking for an IDE specific to IronPython, IronPython Studio is passable. It'll give you some Intellisense functionality, although it is a bit flaky at times, so do not close that MSDN documentation window just yet. It will allow you to interactively debug like any other .Net project. Also it supports the Visual Studio 2008 forms designer if you prefer to make your GUI's that way. I haven't tried it, but it may also support running nUnit tests using TestDriven.net.

Sadly, renames and other refactoring features offered by VS 2008 seem to be unsupported.

Dormancy answered 7/5, 2009 at 2:16 Comment(1)
Thanks for this - I was aware of IPyS, though I haven't tried it, but its abandoned status and support only for IP1 doesn't feel very confidence-inspiring.Corrinecorrinne
F
2

I'm using IPy in an embedded environment for running test scripts.

1 & 2. You are right there is not much support for code editing or refactoring -- proper Visual Studio support would be great.

Ignoring your third point -- interpreted vs compiled is an age-old argument.

Haven't done much of interactive hacking yet, so can't comment on 4th point.

I use the Visual Studio debugger to debug my IPy scripts.

Don't know about a unit test IDE integration either.

Flivver answered 10/5, 2009 at 10:19 Comment(2)
Thanks for the answer, but I think your dismisal of the compiled vs interpreted bit is misplaced. Advocates of (Iron)Python often cite the rapid interactivity of the development as an advantage. The reality is that this is absent with current IPy versions, because the startup time exceeds compile time for small applications.Corrinecorrinne
Hmmm ... you have an example program which illustrates that? Thanks.Flivver
B
2

I am using Iron Python, not professionally, unfortunately, but as a Rapid Development language for my personal projects.

Python is such a great language for ease of use, brevity but readability of code and simplicity. Some of this is due to it being a dynamic language, but also its basic syntax.

I am, however no cpython guru, and can get so much more out of Iron Python than cpython as I can easily leverage the .NET platform, on which I am far more at home. I am disappointed there is currently no port to the Twisted libraries in cpython, the only extended Python libraries I am comfortable with, as they would be so useful in .NET.

I also use the interactive window just to test .NET structures when I am coding in C#.

Broach answered 10/5, 2009 at 10:27 Comment(0)
A
2

I'm running a set of blackjack simulations on Python 2.6 vs. IronPython 2.6, and hype notwithstanding, IronPython seems to be just a little bit slower (maybe by about 10%?) The code is very simple, just stuff like:

def highcount(hand):
  total=0
  for c in hand:
    if c[1]==11 and 11+total>21:
      total+=1
    else:
      total+=c[1]
  return total

and

def dmove():
  if ( (dhand[0][1]==11 and dhand[1][1]==10) or 
       (dhand[0][1]==10 and dhand[1][1]==11) ):
    return 21 # should return blackjack

  while 1:
    lc=lowcount(dhand)
    hc=highcount(dhand)
    if lc>21: return lc # dealer busts
    if lc>=17: return hc # stand on soft 17
    if hc>=17: return hc # stand on hard 17
    dhand.append(deck.pop()) # hit otherwise

If the .NET VM optimizations are similar to the JVM's Hotspot is quite possible that over longer runs, IronPython may run the simulation faster.

Another important point to note is the blackjack simulation easily lends itself to spawning on multiple threads which CPython's GIL makes completely worthless in terms of the speed boost. With IronPython I do get the full 400% speedup on a quadcore, which is really really cool.

The code does not make use of any esoteric Python features (that's a good thing) which meant that it ran 100% unmodified on IronPython.

Artis answered 11/4, 2010 at 17:8 Comment(0)
B
1

I just bought Foord's book so right now I'm only learning it. I really hope to use it for web development but sadly I have to wait until it is supported with asp.net mvc. I also plan to use it as a scripting tool for windows systems since I can utilize the .net framework

Businesslike answered 30/5, 2009 at 15:29 Comment(2)
You might interesting in this https://mcmap.net/q/503855/-ironpython-on-asp-net-mvcLitigable
I'm looking at that, but it seems that MVC with IronPython is still rough around the edges - it seems to only work with Views, and nothing else.Businesslike
A
1

At work we use IronPython against the SharePoint object model. It is perfect for lots of little programs that are bigger than a powershell script but not something that needs to be deployed as an artifact into SharePoint. It is great for rapid prototyping on servers where Visual Studio isn't installed. We typically use Notepad++ as our editor of choice.

Alrzc answered 20/2, 2012 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.