There are two approaches to building extension modules on Windows, just as there are on Unix: use the distutils package to control the build process, or do things manually. The distutils approach works well for most extensions; documentation on using distutils to build and package extension modules is available in Distributing Python Modules. This section describes the manual approach to building Python extensions written in C or C++.
To build extensions using these instructions, you need to have a copy of the Python sources of the same version as your installed Python. You will need Microsoft Visual C++ ``Developer Studio''; project files are supplied for VC++ version 6, but you can use older versions of VC++. The example files described here are distributed with the Python sources in the PC\ example_nt\ directory.
C>is the DOS prompt,
>is the Python prompt; note that build information and various debug output from Python may not match this screen dump exactly):
C>..\..\PCbuild\python_d Adding parser accelerators ... Done. Python 2.2 (#28, Dec 19 2001, 23:26:37) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. >>> import example [4897 refs] >>> example.foo() Hello, world [4903 refs] >>>
Congratulations! You've successfully built your first Python extension module.
"spam"as its first argument (use the minimal example.c in this directory as a guide). By convention, it lives in a file called spam.c or spammodule.c. The output file should be called spam.dll or spam.pyd (the latter is supported to avoid confusion with a system library spam.dll to which your module could be a Python interface) in Release mode, or spam_d.dll or spam_d.pyd in Debug mode.
Now your options are:
In either case, copy example_nt\example.def
to spam\spam.def, and edit the new
spam.def so its second line contains the string
initspam'. If you created a new project yourself, add the
file spam.def to the project now. (This is an annoying
little file with only two lines. An alternative approach is to
forget about the .def file, and add the option
/export:initspam somewhere to the Link settings, by
manually editing the setting in Project Options dialog).
Now open the Project > Settings dialog. You only need to change a few settings. Make sure All Configurations is selected from the Settings for: dropdown list. Select the C/C++ tab. Choose the Preprocessor category in the popup menu at the top. Type the following text in the entry box labeled Addditional include directories:
Then, choose the Input category in the Link tab, and enter
in the text box labelled ``Additional library path.''
Now you need to add some mode-specific settings:
Select ``Win32 Release'' in the ``Settings for'' dropdown list.
Click the Link tab, choose the Input Category, and append
pythonXY.lib to the list in the ``Object/library modules''
Select ``Win32 Debug'' in the ``Settings for'' dropdown list, and
pythonXY_d.lib to the list in the ``Object/library
modules'' box. Then click the C/C++ tab, select ``Code
Generation'' from the Category dropdown list, and select ``Debug
Multithreaded DLL'' from the ``Use run-time library'' dropdown
Select ``Win32 Release'' again from the ``Settings for'' dropdown list. Select ``Multithreaded DLL'' from the ``Use run-time library:'' dropdown list.
You should now create the file spam.def as instructed in the
previous section. Then chose the Insert > Files
into Project dialog. Set the pattern to
*.* and select
both spam.c and spam.def and click OK. (Inserting
them one by one is fine too.)
If your module creates a new type, you may have trouble with this line:
Change it to:
and add the following to the module initialization function:
MyObject_Type.ob_type = &PyType_Type;
Refer to section 3 of the Python FAQ for details on why you must do this.
See About this document... for information on suggesting changes.