views:

522

answers:

3

Is there a way using py2exe or some other method to generate dll files instead of exe files?

I would want to basically create a normal win32 dll with normal functions but these functions would be coded in python instead of c++.

+3  A: 

I am not aware of py2exe being able to do that, as I believe that it does not actually make object symbols out of your Python code, but just embeds the compiled byte-code in an executable with the Python runtime).

Creating a native library may require a bit more work (to define the C/C++ interface to things) with the Python-C API. It may be somewhat easier using Elmer for that.

Tom Alsberg
+1  A: 

I doubt that py2exe does this, as it's architectured around providing a bootstrapping .exe that rolls out the python interpreter and runs it.

But why not just embed Python in C code, and compile that code as a DLL?

Eli Bendersky
+3  A: 

I think you could solve this by doing some hacking:

  • Take a look at the zipextimporter module in py2exe . It helps with importing pyd-files from a zip.
  • Using that, you might be able to load py2exe's output file in your own app/dll using raw python-api. (Use boost::python if you can and want)
  • And, since py2exe's outputfile is a zip, you could attach it at the end of your dll, making the whole thing even more integrated. (Old trick that works with jar-files too.)

Not tested, but I think the theory is sound.

Essentially, you reimplement py2exe's output executable's main() in your dll.

Marcus Lindblom
py2exe has 1 code output path, the main(). Do you have any thoughts on a way to specify the exported functions/classes with python only code? Assuming I made some sort of similar utility py2dll
Brian R. Bondy
No. I don't think you can do it without writing some C. I was thinking that you'd run py2exe as usual, and use it's library output, but code your own dll, which exports a 'my_pymain' function that does something similar to py2exe's executables. You should be able to steal that code from py2exe.
Marcus Lindblom