Extending & Embedding Python Using C - A Module Using Windows
Written by Mike James   
Wednesday, 08 January 2025
Article Index
Extending & Embedding Python Using C - A Module Using Windows
Hello World
Testing

The command line that this generates to build the library is:

cl.exe /Zi /EHsc /nologo /IC:/Users/user/AppData/
   Local/Programs/Python/Python311/include 
   C:\Users\user\Documents\projects\arith.c 
   "/link /dll /OUT:arith.pyd 
    /LIBPATH:C:/Users/user/AppData/
            Local/Programs/Python/Python311/libs"

This works and finds the necessary include files.

This should now all work but the Intellisense prompting will still be showing an error in the editor. The Intellisense analysis of your code is controlled by the c_cpp_properties.json file. You can add an autogenerated file using the command C/C++: Edit Configurations (UI) from the command palette. To include the Python header you need to add the line:

"C:/Users/user/AppData/Local/Programs/Python/
Python311/include/"

to the include path in c_cpp_properties.json giving:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/Users/user/AppData/Local/
                      Programs/Python/Python311/include/"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            
            "windowsSdkVersion": "10.0.22000.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

Now not only should the program compile, it should also show no errors in the editor. The best way to compile it is to use Terminal, Run Build Task as this doesn’t attempt to start or debug an executable. After the build you should find arith.pyd in the same folder as the program.

If you see an error something like:

fatal error LNK1112: module machine type 
'x64' conflicts...

you have started VS Code using the wrong Native Tool prompt.

Testing the Module

The final check is to make sure that the new module can be used within a Python program. You can do this in VS Code.


To make running Python programs easy you first need to install the Microsoft Python extension which automatically installs Pylance:

VSC4

Now you can add a new file to the project called test.py with the code:

import arith
print(arith.add(1,2))
print(dir(arith))

As the current project folder isn’t on the Python import path, Pylance will mark the import as an error. To put the current folder on the import path we need to use the extension settings command. Use the command File, Settings and then select Pylance in the list of extensions.


You need to add the workspaceFolder to the Analysis:include setting:

VSC5

If you do this then the Pylance error on the import statement will vanish.

When you run the program you should see:

3
['__doc__', '__file__', '__loader__', '__name__',
'__package__', '__spec__', 'add']

Notice that the function has the standard methods as well as the custom add function.

In chapter but not in this extract

  • Multimodal Debugging

extendPython180

Summary

  • The Windows development system is slightly harder to use because it doesn’t use the GCC and gdb debugger. C programmers are generally less familiar with the Microsoft C compiler and its associated debugger.

  • Although you can use the command line or any IDE, there are advantages in using VS Code as it is open source and supports both Python and C.

  • It is worth checking that everything is installed correctly by creating a Hello World program.

  • To build a Python extension you have to ensure that an include directory has been specified and that a DLL is to be generated. The DLL has the extension .pyd and not the usual .dll.

  • To make the Intellisense prompting work correctly you have to define a c_cpp_properties.json file.

  • Testing an extension is a matter of running a Python program that imports it and then makes use of it.

  • At some point you are going to need to work out how to debug an extension. This involves running a debugger for the Python program and a separate debugger for the C program.

  • If you want to debug the C API as well as your own code, you need to download a symbols file and the source code and modify the launch.json file.

 

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 ***NEW!!!
  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

<ASIN:B0CK3X93KF>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Ruby 3.4 Improves YJIT
06/01/2025

Ruby 3.4 has been released. This version uses the Prism parser as the default, adds an "it" block parameter reference and brings Happy Eyeballs Version 2 support to the socket library.



50 Years Of the Intel 8080
05/01/2025

The Intel 8080 was the very first multi-purpose microprocessor and as such played a pivotal role in the evolution of personal computing. 2024 was the 50th anniversary of the chip that influenced  [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Wednesday, 08 January 2025 )