The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Creating a dll from a python code?

Hi,

I use this very nice python library which has some unique features no other library offers. Now I want to expose the features from this library to another language (say Delphi or VB or whatever).

Currently I know how to create an exe file from the .py file, but this is not working in my scenario.

I was thinking perhaps creating some tcp/ip server, which exposes the .py functionality, but I feel it will be a lot better for me just have a dll. Redistributing python.exe and the other files is not a problem or me.

Any ideas?
WestSorkin Send private email
Saturday, January 13, 2007
 
 
D. Lambert Send private email
Saturday, January 13, 2007
 
 
I don't think so this is what he is looking for as IronPython cannot access classic python libs.

Anyway, if your code is not for high performance application, you can have python scripts in binary form (.exe) and access it's functions within other program via console/command line. That's the easiest way.
lubos
Saturday, January 13, 2007
 
 
re: its
lubos
Saturday, January 13, 2007
 
 
sorry Lambert, my bad. IronPython seems to be much more than I expected.
lubos
Saturday, January 13, 2007
 
 
No offense taken.  I wasn't sure -- I'd just heard a lot of impressive things about it.
D. Lambert Send private email
Saturday, January 13, 2007
 
 
Ok, from a quick look into IronPython's Wiki, it seems it can produce dll's. I will look more at it, but are there any dependencies with .Net or problems with the produced dll?

From the Wiki (http://www.codeplex.com/IronPython/Wiki/View.aspx?title=Samples):

"IronPython’s Hosting APIs can be used to compile Python scripts into DLLs, console executables, or Windows executables. The pyc.py script included in this tutorial leverages these hosting APIs and can be used to compile other Python scripts. It provides a variety of flags such as the ability to specify the target platform of the .NET assembly (e.g., x64)."

And then:

"While the assemblies produced by the IronPython Hosting APIs are true .NET assemblies, the dynamic nature of the Python language makes it difficult to use these from other .NET languages. In short, this means that attempting to import Python types into other .NET languages such as C# is not recommended."

A am not quite keen with the .Net stuff, are assemblies = dlls, which need .Net to be executed?
WestSorkin Send private email
Saturday, January 13, 2007
 
 
I think the standard way to use Python from VB or Delphi is to expose the Python objects in a simple COM server.

You might want to spring for a copy of O'Reilly's "Python Programming on Win32", which is chock full of examples accessing Python from c++, Classic VB, and Delphi.  Great book:
http://www.amazon.com/Python-Programming-WIN32-Mark-Hammond/dp/1565926218/sr=8-1/qid=1168713886/ref=sr_1_1/002-9663122-3491255?ie=UTF8&s=books

ActiveState docs have a lot of documentation on the Python Win32 extensions and stuff you'd find helpful.  Best place to start is probably here:
http://aspn.activestate.com/ASPN/docs/ActivePython/2.2/PyWin32/html/com/win32com/HTML/QuickStartServerCom.html

This looks like good snippet of info on accessing Python COM object from VB, probably obvious from here how you could do it with Delphi too:
http://mail.python.org/pipermail/chicago/2006-April/000626.html

For Delphi you could also use the excellent Python4Delphi components, which makes things easy and add a higher level of interactivity between Delphi and Python.  Main tutorial webpage seems to be down; I don't remember whether same material is included in P4D docs or not:
http://mmm-experts.com/Products.aspx?ProductID=3
http://membres.lycos.fr/marat/delphi/python.htm

There's an active Python for Delphi newsgroup at Yahoo:
http://tech.groups.yahoo.com/group/pythonfordelphi/messages/1234?xm=1&o=1&m=p&tidx=1
Herbert Sitz Send private email
Saturday, January 13, 2007
 
 
Yep, COM server is the way to go.  The caveats are that you'll need python installed on the machine to use it, and you won't get a single .dll, if those issues matter to you.
Grant
Saturday, January 13, 2007
 
 
I think you can use py2exe to create a stand alone dll.
Tony Edgecombe
Sunday, January 14, 2007
 
 
Interesting, here's link to page with some info on use py2exe to create dll:
http://www.py2exe.org/index.cgi/Py2exeAndWin32com

Looks like you have to make the Python part a com server in any case.  But if the py2exe part works well I assume it lessens possible issues with deployment. 

Deployment of python com servers as modules running under a regularly installed interpreter was not that hard, but I recall reading of some problems if the install machine had an already installed Python interpeter of an earlier version.  There's a Python4Delphi site that had quite a bit of info about deploying, but it seems to be down right now.
Herbert Sitz Send private email
Sunday, January 14, 2007
 
 
Herbert Sitz, thank you very much for the help and links.

P4D seems to be the way for me to do what I need. I will look too in possibility of creating a simple tcp/ip server which will expose the needed methods for me.

The only concern I have is the performance. On one hand I have a dll, on the other tcp/ip server.

Any advice on that?
WestSorkin Send private email
Monday, January 15, 2007
 
 
Elmer ( http://elmer.sourceforge.net/
 ) can generate C-callable wrappers for Python code, and allegedly you can then build the result as a a DLL (but I can't offhand find a reference to that).
Richie Hindle Send private email
Monday, January 15, 2007
 
 
WestSorkin -- I don't quite understand.  If you're rolling your own simple tcp/ip server then you have no need to use COM or a dll.  (COM servers are not tcp/ip servers, as far as I know).  COM seems to me like the way to go; that's method that the Python Win32 extensions are made for.  I think you're restricted to out-of-process COM servers, so speed might be slightly slower than a dll, unless you're doing lots and lots of calls to Python I would guess it's not going to big difference:
http://www.ddj.com/184403442?pgno=7

I have no actual experience with this stuff (other than fooling around with py4delphi), but I'd say it's simple enough just to try for yourself and see how performance is.  The py2exe method is still an out-of-process COM server, I'm pretty sure, even though it generates a dll, so I wouldn't expect it to be paster than p4d.  The c-style-wrapper might be worth checking out, but I would suggest sticking with the well-proven and easy-to-use Py4Delphi if you're working in Delphi.
Herbert Sitz Send private email
Monday, January 15, 2007
 
 
Sorry, I would guess that a COM server (even out of process) is going to be faster than a tcp/ip server you roll own your own.  Especially if that tcp/ip server is itself written in Python.  Also easier because you don't have to manage the separate server process.  And I assume the p4d solution also simplifies some issues you'd otherwise have with preserving datatypes.
Herbert Sitz Send private email
Monday, January 15, 2007
 
 
One more post:  here's link to best online tutorial and other info on using Python for Delphi, was down yesterday but now is working:
http://www.atug.com/andypatterns/pythonDelphiTalk.htm

I'm not even sure that p4d uses an out of process com server, since it links to the main Python_xxxx dll.  In any case, if you're programming with Delphi I'm sure p4d is better solution than a separate tcp/ip server.  And going the com-server route seems preferable to me even if you're integrating with vb or vb.net, c#, etc.
Herbert Sitz Send private email
Monday, January 15, 2007
 
 
For anyone who cares, I did some fooling around tonight with Python for Delphi.  The simple Python 'add' function below was running at rate of around 150,000 calls/sec on my modest test machine:

def Add(a, b):
  return a + b

I'm pretty sure that's between 10 and 100 times faster than you'd get from a tcp/ip server.

Also, if you download Py4Delphi make sure you get it from this site, which I think is only site with py4d version that works with Python 2.5:
http://mmm-experts.com/Products.aspx?ProductID=3
Herbert Sitz Send private email
Monday, January 15, 2007
 
 
Herbert,

To be honest, before I post I did not expect any break with my problem, because I did a couple of Google and Yahoo-groups searches without any success.

But now, thanks to your invaluable help, I will be able to do  what I wanted.

Thank you very, very much!

And yes, I did downloaded the p4d from http://mmm-experts.com site. It's funny, because I was using PyScripter from the same site, but I have downloaded it a long time ago :)
WestSorkin Send private email
Tuesday, January 16, 2007
 
 
Thanks a lot.  I'm glad to be of help; it just so happened that I knew a little bit about this and wanted to get back up to speed on it.  Python for Delphi has been mentioned a number of times on JoS.

I have to admit that I don't understand a lot of what's going on in the Py4Delphi demos.  It strikes me that most of them are for a higher level of interaction between Delphi and Python than what I (or you) might need.  Easiest way to simply use a python module as a library seems to me to be illustrated in Demo 25.  Try searching in frmMain.pas for 'mymodule' and the few lines where it's referenced.  I could be wrong, but that seems to point the way towards very easily making use of your Python library.

Still seems to me like Py4Delphi is best route if all you need is to call python from Delphi.  But the route of just making your python module into a little com server also seems pretty easy and you could use that to call from just about any language.

I'd be curious to hear how things go in getting this implemented.
Herbert Sitz Send private email
Tuesday, January 16, 2007
 
 
Something else to try is the Pyrex package at http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/

It is used to combine C and Python then produce a C python extentsion module.

Python extension modules are simply DLL's with a different file extension and an init function.

Now I haven't tried calling these from C, but in theory it could be done.
Kevin Dahlhausen Send private email
Wednesday, January 17, 2007
 
 
The third option (used by a couple of games out there) would have been to simply "embed" the python engine in your program.

There is some extensive documentation here: http://docs.python.org/api/embedding.html
Guillaume Proux
Monday, January 29, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz