views:

88

answers:

2

I'm an extension noob. What I want to do is create an extension that doesn't require other libraries to be installed. Is this impossible because the extension has to link against a specific version of libpython at runtime?

+3  A: 

You can't make a statically linked extension module because Python needs to load it dynamically at runtime and because (as you reasoned) the module needs to dynamically link against libpython.

You could compile your own custom version of Python with your extension statically linked into the interpreter. That's usually more trouble than it's worth.

Why do you want to make a statically linked extension? If we have more information about your goals, we might be able to help you achieve them in a different way.

Welcome to StackOverflow. :-)

Daniel Stutzbach
Thanks Daniel, for the response and welcome :) I guess I'm trying to figure out the best way to distribute an app (that has C extensions) to the major platforms without requiring compilation at installation time.
Andrew Moffat
@Andrew: Ah! Why didn't you say so? :) Use the distribute package (http://pypi.python.org/pypi/distribute) and build eggs (`setup.py bdist_egg`). For Windows, build an installer (`setup.py bdist_wininst`). Watch out for Unicode incompatibility problems (see http://bugs.python.org/issue8654).
Daniel Stutzbach
Or, if your extension wraps something that is commonly already found on these platforms, use ctypes instead of an extension module.
fraca7
Thanks so much guys
Andrew Moffat
+2  A: 

I think you're mixing things. You don't want the extension to be statically linked in the interpreter (which is possible but cumbersome since it involves rebuilding a custom interpreter), you want your extension not to be linked against pythonxx.dll, or to be linked statically to it. This is not possible; your extension and the python interpreter would have each their own copies of global variables for instance, which is Bad.

There is another approach, which is to determine what Python versions are available at runtime and using dynamically the Python/C API by loading the Python DLL through LoadLibrary (Windows) or dlopen (Linux/etc), then deciding at runtime on the methods signatures depending on the version, etc. Very cumbersome. For an example of this kind of manipulation in Delphi, see PythonForDelphi:

http://www.atug.com/andypatterns/pythonDelphiTalk.htm

I'm not aware of any other project who would do that.

fraca7
Thanks you, I'll look into the dynamic loading method.
Andrew Moffat