4.1 A Cookbook Approach

#
4.1 A Cookbook Approach

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 7.1, but you can use older versions of VC++. Notice that you should use the same version of VC++that was used to build Python itself. The example files described here are distributed with the Python sources in the PC\example_nt\ directory.

1. Copy the example files
   The example_nt directory is a subdirectory of the PC directory, in order to keep all the PC-specific files under the same directory in the source distribution. However, the example_nt directory can't actually be used from this location. You first need to copy or move it up one level, so that example_nt is a sibling of the PC and Include directories. Do all your work from within this new location.

2. Open the project
   From VC++, use the File > Open Solution dialog (not File > Open!). Navigate to and select the file example.sln, in the copy of the example_nt directory you made above. Click Open.

3. Build the example DLL
   In order to check that everything is set up right, try building:

   1. Select a configuration. This step is optional. Choose Build > Configuration Manager > Active Solution Configuration and select either Release orDebug. If you skip this step, VC++ will use the Debug configuration by default.

   2. Build the DLL. Choose Build > Build Solution. This creates all intermediate and result files in a subdirectory called either Debug or Release, depending on which configuration you selected in the preceding step.

4. Testing the debug-mode DLL
   Once the Debug build has succeeded, bring up a DOS box, and change to the example_nt\Debug directory. You should now be able to repeat the following session (`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.

5. Creating your own project
   Choose a name and create a directory for it. Copy your C sources into it. Note that the module source file name does not necessarily have to match the module name, but the name of the initialization function should match the module name -- you can only import a module `spam` if its initialization function is called `initspam()`, and it should call `Py_InitModule()` with the string `"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:

   1. Copy example.sln and example.vcproj, rename them to spam.*, and edit them by hand, or
   2. Create a brand new project; instructions are below.

   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 Properties dialog).

6. Creating a brand new project
   Use the File > New > Project dialog to create a new Project Workspace. Select Visual C++ Projects/Win32/ Win32 Project, enter the name ("`spam`"), and make sure the Location is set to parent of the spam directory you have created (which should be a direct subdirectory of the Python build tree, a sibling of Include and PC). Select Win32 as the platform (in my version, this is the only choice). Make sure the Create new workspace radio button is selected. Click OK.

   You should now create the file spam.def as instructed in the previous section. Add the source files to the project, using Project > Add Existing Item. Set the pattern to `*.*` and select both spam.c and spam.def and click OK. (Inserting them one by one is fine too.)

   Now open the Project > spam properties 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 General category in the popup menu at the top. Type the following text in the entry box labeled Additional Include Directories:

       ..\Include,..\PC

   Then, choose the General category in the Linker tab, and enter

       ..\PCbuild

   in the text box labelled Additional library Directories.

   Now you need to add some mode-specific settings:

   Select Release in the Configuration dropdown list. Choose the Link tab, choose the Input category, and append `pythonXY.lib` to the list in the Additional Dependencies box.

   Select Debug in the Configuration dropdown list, and append `pythonXY_d.lib` to the list in the Additional Dependencies box. Then click the C/C++ tab, select Code Generation, and select Multi-threaded Debug DLL from the Runtime library dropdown list.

   Select Release again from the Configuration dropdown list. Select Multi-threaded DLL from the Runtime library dropdown list.

If your module creates a new type, you may have trouble with this line:

        PyObject_HEAD_INIT(&PyType_Type)

Change it to:

        PyObject_HEAD_INIT(NULL)

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.