Linux/Unix > Python Essential Reference, Second Edition > B. Extending and Embedding Python > Compilation of Extensions | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105094192217201128182154

### Compilation of Extensions

Extension modules are usually compiled into shared libraries or DLLs that can be dynamically loaded by the interpreter. The low-level details of this process vary by machine, but the `distutils` module in the Python library can be used to simplify the process. To create an extension module using `distutils`, follow these steps:

1. Create a file called `setup.py` that starts with the following code:

       # setup.py
       from distutils.core import setup, Extension

2. Add some source information about your extension as follows:

       setup(name="spam", version="1.0",
             ext_modules=[Extension("spam", ["spam.c", "spamwrapper.c"])])

3. To build your extension, type the following:

       python setup.py build

At this point, a shared library such as `spammodule.so` (or some variant of this name such as `spammodule.sl` or `spammodule.dll`) will be created in a special “build” directory. If you want to install the extension, you can type `python setup.py install`. This command will copy the shared library to the `site-packages` directory (for example, `/usr/local/lib/python2.1/site-packages`).

If you need to supply additional build information such as include directories, libraries, and preprocessor macros, they can also be included in the `setup.py` file as follows:

    setup(name="spam", version="1.0",
       ext_modules=[
         Extension(
            "spam",
            ["spam.c", "spamwrapper.c"],
            include_dirs = ["/usr/include/X11","/opt/include"],
            define_macros = [('DEBUG',1'),
                             ('NEED_CPLUSPLUS',0)],
            undef_macros = ['HAVE_FOO','HAVE_NOT'],
            library_dirs= ["/usr/lib/X11", "/opt/lib"],
            libraries = ["X11", "Xt", "blah"]
          )
       ]
    )

At this point, it’s worth noting that the `distutils` module can more generally be used to create Python packages suitable for distribution and installation by other users. For instance, it allows packages to be distributed as a mix of scripts and compiled extensions. It also knows how to create RPM spec files and self-extracting ZIP files on Windows. Further details about the `distutils` module are available at http://www.python.org/doc/current/dist/dist.html.

In some situations, you may want to build an extension module manually. This almost always requires advanced knowledge of various compiler and linker options. The following is an example on Linux:

    linux % gcc -c -fpic -I/usr/local/include/python2.1 spam.c spamwrapper.c
    linux % gcc -shared spam.o spamwrapper.o -o spammodule.so

When building a module, it’s important to note that the name of the shared library must match the name of the module used in the wrapper code. For example, if the module is named `spam`, the initialization function must be named `initspam` and the shared library must be called `spammodule.so` (possibly with a different file extension, depending on your machine).

Once compiled, an extension module is used like any other module, by simply using the `import` statement:

    % python
    Python 2.0 (#1, Oct 27 2000, 14:34:45)
    [GCC 2.95.2 19991024 (release)] on sunos5
    Type "copyright", "credits" or "license" for more information..
    >>> import spam
    >>> spam.gcd(63,56)
    7
    >>> spam.gcd(71,89)
    1
    >>> spam.print_data(name="Dave",phone="555-1212")
    Name    : Dave
    Email   : None
    Phone   : 555-1212
    >>>

When searching for an extension module, Python uses the same search path as it uses for `.py` files. Thus, to properly find an extension module, it should be located in the current working directory or in one of the directories in `sys.path`.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.