Computer Magic
Software Design Just For You
 
 

Python in .NET?


What is it?

Ever try to embed Python into your C or C++ program? Have you ever tried to embed Python into any program? It isn't the easiest thing in the world. The difficulty is compounded when you try to embed it into a managed language such as C#.

To expose the Python functionality to managed languages, I used Managed C++. It is still C++, but it has one leg in the traditional C++ arena, and one leg in the new .NET Managed arena. This allows the managed app (written in C# in this case) to access the code created in the managed C++ environment. It is basically a set of wrapper classes.

The end result is an API-like interface that helps simplify the steps required to embed Python into a managed environment. Pointer allocation and removal and such should happen automatically. In addition, basic type conversions can be done quickly and easily.

Why would you do this?

Well, it seemed like a good idea at the time... Actually, I wanted to try out a few things. First, I wanted to try out some DirectX programming via a managed language (C# in this case). In addition, I had planned to use Python as an embedded language for a 3D engine I am working on. This can greatly simplify many aspects of the game development and avoid constant recompilations. Lastly, I have used C/C++ for many years, but at this point had never tried out the managed C++ environment (which is quite nice if you don't mind me saying so) and I thought it was a good project to get my feet wet with.

Should I use this for my application?

Shucks, that's up to you. It is an early Beta. Does it work? YES!!! But should you use it? I slapped it together and got it working as fast as possible, so I am sure that there are some problems here and there. Also, read below about the lack of some features including access to the Dictionary objects. On the other hand, it seems to be fairly stable in the limited demos that I have created.

Which version of Python are you using?

It is built using Python 2.4. You could probably substitute a preferred version simply by changing the reference to the DLL in the project (I didn't get too fancy). You will also need the version you want to work with installed on your system for things to compile correctly. When distributing your software, be aware that you may need to include the Python DLL and any modules that you utilize. I get around module issues in one project by storing them in my database and loading them at runtime before they are needed, but that is just me in a particular situation which may not work for everyone.

Does it work in Windows?

Hehe, if you have to ask that, you probably aren't ready to embed anything into the .NET Framework. .Net is a framework that was written to run on MS platforms. There are versions of the .NET CLR (Common Runtime Library) for *NIX systems. I have not tried this in a *NIX environment, I would be interested to hear if it works.

Which compiler did you use?

I used .NET 2003 and it compiles in that. It may work in other versions of the .NET IDE (2002, 2005) but I haven't tried them. Good luck with that.

Do I have to expose Python API functions like I do when using the Python API directly?

Yes, in .NET we can define a function layout but now we call them delegates. There is a standard function definition you have to use. It isn't quite as bad as it sounds. The objects available simplify and shorten the code needed to extract function parameters and to convert things to and from their proper types (remember Python doesn't have Int32). For those who are already familiar with the Python API, it is basically a condensed version. The same steps are needed, but they can be done with fewer lines of code because of the object oriented nature of the .NET framework. For example, the Tuple class allows you to get or set an item based on its position. In addition, the conversion is automatic depending on which method you use.

Do I have to build up one of those function tables to send to Python?

Nope. Python doesn't actually need a complete pre-built function table all laid out before you start its engines. You can actually send it multiple function tables, you just have to make sure that the memory allocated for that function table isn't released while the program is running! That is why the embedding examples always have the function table allocated as a global variable (that way they aren't released until the program ends. I learned this the hard way with a function table allocated on the stack of a local method). Allocating the table on the heap also works. With this library, you can add new API functions later in the program because each time you call the addAPIFunction function, it allocates a new function table on the heap. You just use the addAPIFunction method to make your functions available to Python. Since each function table is very small, the memory overhead is minimal but needed.

You have Tuples, where is the dictionary support?

Yeah, it isn't there yet. Like I said, I slapped it together real quick. I am sure it will be added later by myself or someone like me, or some one like you, or someone like someone else, or if not them, by some one totally opposite.

Can I get a copy of your code?

Sure thing. The code is licensed under the LGPL license. That means you can't sell it, but you can use it and modify it to your heart's content, and you can use it in commercial applications for free if you give me some credit. The download link is at the bottom of the page. Help yourself. Also, if you make some great program by using my code, I would enjoy a free copy so I can check out what you have done.

Can I help with this code?

Sure thing. I would welcome any suggestions or help. Just make sure you understand the terms of the LGPL license and that you know that any enhancements will be merged into the source tree at my discretion and convenience. Also, you relinquish any rights or ownership of the code you write when you submit it to me for inclusion in the source tree (actually it is part of the LGPL license, read it, once its licensed this way, it kind of becomes its own entity). But hey, with that legal jargon out of the way, open source makes some of the world go round, eh?

Where are the examples?

Yeah, sorry about that. Busy schedule at the moment keeps me from writing any, and my examples currently are part of projects intended for commercial retail. You can use the Object browser and the Python API documentation to figure out what steps you need to take. Someone who has worked with the C API already should be able to tell what they need to do by just using the Object browser. Like I said, the steps are the same, it is just a condensed version.

I'm stuck, can I contact you directly for help?

Of course you can, if you are willing to pay our hourly going rate. Unfortunately, I just don't have enough free time to help everyone; unless you are paying of course. In fact, e-mail inquiries that do not start with your credit card number are likely to be ignored (Don't actually do that, I'm just joking, it's terribly insecure!). You could try checking our forums for free help. I will hang out there and try to help where I can, but my availability depends on what is going on that week.

Download Link - Finally...

By downloading Python.NET you agree to the terms of the LGPL license. You also agree that Computer Magic is in no way liable for any failure or success attributed to this product and will not be held responsible for any damages (real or imagined) that may or may not result from use of this product. If you do not agree, you may not download or use this code in any way shape or form.

Do you agree to abide and be bound by the LGPL license?

  • 7/21/2005 - Download Python.NET Version 0.1.0 (I Agree to the terms and conditions!)


  • Home | My Blog | Products | Edumed | About Us | Portfolio | Services | Location | Contact Us | Embedded Python | College Courses | Quick Scan | Web Spy | EZ Auction | Web Hosting
    This page has been viewed 35593 times.

    Copyright © 2005 Computer Magic And Software Design
    (360) 417-6844
    computermagic@hotmail.com
    computer magic