This is an old copy of the Python FAQ. The information here may be outdated.

How can I embed Python into a Windows application?

The easiest way to embed a Python interpreter in a Windows application is to use a standard Python DLL (e.g. python25.dll), and link it into your application. By using a standard DLL, you can use both Python modules and existing extension modules with your application.

To link the DLL into your application, you can either use an import library (the python.org distribution includes import libraries in the ./libs directory), or use LoadLibrary to load the DLL on the fly, and use GetProcAddress to dig out the necessary API functions. The latter is more work, but can give you a more flexible solution (e.g. you can make your application work even if no Python DLL is available, or even support multiple Python versions with a single EXE).

For information on how to initialize and configure the embedded Python interpreter, see the standard Extending and Embedding the Python Interpreter (dead link) documentation.

Notes #

If you mix compilers or run-time libraries, some Python API functions may not work properly. Most notably, functions that take FILE* arguments may crash or otherwise misbehave, and should be avoided. See the following article for more on this:

pyrun-simplefile-crashes-on-windows-but-not-on-unix-why

The Py_None macro sometimes causes similar problems, since it expands to a reference to a data structure (_Py_NoneStruct) that’s defined inside the Python DLL. To create a new reference to Py_None without relying on _Py_NoneStruct, use:

object = Py_BuildValue("");

CATEGORY: windows

 

A Django site. rendered by a django application. hosted by webfaction.