Skip to content

DahlitzFlorian/extending-python-with-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extending Python with C

black

Description

This repository contains a examples revealing how to extend Python with C code. It's inspired by a Medium article from Matthias Bitzer and the Python Documentation.

Usage

If you want to run the modules in Python, you can install them using the provided setup.py. I recommend creating a virtual environment before installing them.

Unix

$ python -m venv venv
$ source venv/bin/activate

Windows

$ python -m venv venv
$ venv\bin\activate

Installation

The following command will install the modules in the site-packages directory of your current environment.

$ python setup.py install

You can also only build the modules using:

$ python setup.py build

This creates a new directory called build. Change your working directory to the one containing the .so libraries (.dll under Windows). Creating a Python session in this directory gives you access to the modules even though they are not installed.

Docker

If you want to test the modules in a Docker container to not mess up your own environment, you can run the following commands:

$ docker image build -t cmodules .
$ docker container run --rm --name cmodules -it cmodules

After running the container a Python REPL is started were you can import the necessary modules. An example is given below.

Python 3.7.3 (default, Mar 27 2019, 23:40:30) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ccodemath import factorial
>>> factorial(6)
720
>>> factorial("6")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>> quit()

Available Modules

Module Name Description
ccodemath A collection of mathematical functions