Extending & Embedding Python Using C - A Module Using Linux
Written by Mike James   
Monday, 06 May 2024
Article Index
Extending & Embedding Python Using C - A Module Using Linux
GCC and VS Code
Building a Python Extension
Testing the Module
Multimodal Debugging

Getting started with extending Python isn't easy because of the need to set everything up correctly. Not if you follow our instructions for the Linux toolchain. This is an extract from the new book by Mike James that helps you combine the speed and power of C with the versatility and ease-of-programming of Python.

Extending & Embedding Python Using C

By Mike James

extendPython360

Buy from Amazon.

Contents

       Preface

  1. Extending And Embedding Python
  2. A First C Module Using Linux 
  3. A First C Module Using Windows
  4. Module Basics
        Extract: A First Module
        Extract: 
    Pi 
  5. Arguments
  6. Returning Python Objects
  7. Objects And Attributes
  8. More Complex Objects – Tuples, Lists and Dicts
  9. Errors, Exceptions And Reference Counting
        Extract:
    Exceptions 
  10. Bytes And Strings
  11. Modules And Attributes
  12. New Types
  13. Advanced Types
  14. Threads And The GIL
  15. Embedding Python ***NEW!!!

<ASIN:B0CK3X93KF>

Starting to create Python extensions can be challenging. Once you have your first, even trivial, extension up and running then you can make steady progress to your finished product. Getting started however has a high barrier and none of the existing documentation is particularly helpful. It all looks easy until you actually try it and then the range of things that can go wrong with the configuration is very wide and very difficult to troubleshoot.

In this chapter we concentrate on getting a Linux-based development system up and running. This is in general an easier task than getting a Windows-based development system up and running and, if you can, it is worth starting with Linux, even if your final target is Windows. In practice, you generally want to develop an extension for both Windows and Linux to allow as many people as possible to make use of it.

If you have a choice start with Linux.

You can develop extensions using nothing but the command line, but it is much easier to use an IDE or a code editor. If you are an experienced C programmer you probably have your own preferences and a development environment already set up. In this case you can easily adapt the instructions given in this chapter to your own setup simply by considering what compiler options and settings are in use.

There are Python modules designed for creating extensions and packaging them. The best known, and the one most prominent in the documentation, is distutils. This is is deprecated, to be removed in Python 3.12. Its functionality for specifying package builds is replaced by third-party packages, setup tools and packaging, For reasons of simplicity and clarity these packages are not used in this book. If you need to create a Python package you might find setup tools useful.

In the rest of this chapter, and throughout this book, Visual Studio Code (VS Code) is used as the development environment. The reason for this is that it is open source, relatively easy to use and supports a range of languages including C and Python. To make the procedure as foolproof as possible we’ll first check that it is possible to compile a simple C program and then move on to compiling and using a simple extension.

For simplicity the basic C/C++ VS Code extension is used to compile the test extension. For a real project you are most likely going to need to use a make system. VS Code has good support for CMake and this or Make is what you can use to to work with multi-file projects. For simplicity, the examples do not use CMake or Make as they are all single file projects.



Last Updated ( Tuesday, 07 May 2024 )