# Python Programming/Extending with C

| Previous: MS Word | Index | Next: Extending with C++ |
Python modules can be written in pure Python but they can also be written in the C language. The following shows how to extend Python with C.

## Using the Python/C API
### A minimal example

To illustrate the mechanics, we will create a minimal extension module containing a single function that outputs "Hello" followed by the name passed in as the first parameter.
We will first create the C source code, placing it to hellomodule.c:
Then we will need a setup file, setup.py:
Then we can build the module using a procedure whose details depends on the operating system and the compiler suite.

#### Building with GCC for Linux

Before our module can be compiled, you must install the Python development headers if you have not already. On Debian and Debian-based systems such as Ubuntu, these can be installed with the following command:
On openSUSE, the required package is called python-devel and can be installed with zypper:
Now that Python.h is available, we can compile the module source code we created in the previous section as follows:
The will compile the module to a file called hello.so in build/lib.linux-i686-x.y.

#### Building with GCC for Microsoft Windows

Microsoft Windows users can use MinGW to compile the extension module from the command line. Assuming gcc is in the path, you can build the extension as follows:
The above will produce file hello.pyd, a Python Dynamic Module, similar to a DLL. The file will land in build\lib.win32-x.y.
An alternate way of building the module in Windows is to build a DLL. (This method does not need an extension module file). From cmd.exe, type:
where XY represents the version of Python, such as "24" for version 2.4.

#### Building using Microsoft Visual C++

With VC8, distutils is broken. Therefore, we will use cl.exe from a command prompt instead:

#### Using the extension module

Change to the subdirectory where the file hello.so resides. In an interactive Python session you can use the module as follows.

### A module for calculating Fibonacci numbers

In this section, we present a module for Fibonacci numbers, thereby expanding on the minimal example above. Compared to the minimal example, what is worth noting is the use of "i" in PyArg_ParseTuple and Py_BuildValue.
The C source code in (fibmodule.c):
The build script (setup.py):
Usage:

## Using SWIG

SWIG is a tool that helps a variety of scripting and programming languages call C and C++ code. SWIG makes creation of C language modules much more straightforward.
To use SWIG, you need to get it up and running first.
You can install it on an Ubuntu system as follows:
To get SWIG for Windows, you can use binaries available from the SWIG download page.
Once you have SWIG, you need to create the module source file and the module interface file:
hellomodule.c:
hello.i:
Then we let SWIG do its work:
The above produces files hello.py and hello_wrap.c.
The next step is compiling; substitute /usr/include/python2.4/ with the correct path to Python.h for your setup:
As the last step, we do the linking:
The module is used as follows:

## External links

* Extending and Embedding the Python Interpreter, python.org
* Python/C API Reference Manual, python.org
* SWIG, swig.org
* Download SWIG, swig.org
| Previous: pyFormex | Index | Next: Extending with C++ |