https://github.com/dahlitzflorian/extending-python-with-c
Extending Python with C
https://github.com/dahlitzflorian/extending-python-with-c
c educational python python-3 python-c-api python3
Last synced: 2 months ago
JSON representation
Extending Python with C
- Host: GitHub
- URL: https://github.com/dahlitzflorian/extending-python-with-c
- Owner: DahlitzFlorian
- License: apache-2.0
- Created: 2019-04-13T09:31:12.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-14T19:53:14.000Z (about 6 years ago)
- Last Synced: 2025-01-08T13:27:56.763Z (4 months ago)
- Topics: c, educational, python, python-3, python-c-api, python3
- Language: C
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Extending Python with C

## Description
This repository contains a examples revealing how to extend Python with C code.
It's inspired by a [Medium article][mediumarticle] from [Matthias Bitzer][authorcredit] and the [Python Documentation][pythondocs].## 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
```shell
$ python -m venv venv
$ source venv/bin/activate
```### Windows
```shell
$ python -m venv venv
$ venv\bin\activate
```### Installation
The following command will install the modules in the `site-packages` directory of your current environment.
```shell
$ python setup.py install
```You can also *only* build the modules using:
```shell
$ 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:
```shell
$ 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.```shell
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 "", line 1, in
TypeError: an integer is required (got type str)
>>> quit()
```## Available Modules
| Module Name | Description |
|-------------|-------------|
| ccodemath | A collection of mathematical functions |[authorcredit]: https://medium.com/@matthiasbitzer94
[mediumarticle]: https://medium.com/@matthiasbitzer94/how-to-extend-python-with-c-c-code-aa205417b2aa
[pythondocs]: https://docs.python.org/3/extending/extending.html#a-simple-example