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

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.

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

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 Anaylsis:include setting:

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.

Getting the Latest Python

Linux distributions vary in how up-to-date their standard version of Python is. If you are developing an extension then it makes sense to work with the latest stable version of Python. If you have already got GCC installed and working then the effort to download and compile the latest Linux is small.

If you want to make use of the Python debugger you need to make sure that libffi-dev is installed:

sudo apt-get install libffi-dev

As long as it is, Python will be compiled to support the debugger. The next step is to download the source code from the Python website and uncompress it. It doesn’t matter where you extract it to as you are going to use it to compile the Python system and after that you can delete it.

Once you have changed directory to the uncompressed source files you simply use the commands:

./configure --enable-optimizations
sudo make altinstall

The first takes a few minutes and the second tens of minutes. Any errors usually relate to missing dependencies and to fix them all you have to do is install the missing components and recompile.

In most cases your new version of Python will be installed in usr\local in the bin, include and lib subdirectories.

To make sure that you get the latest version of Python when you run something from the command line you will usually have to update a link stored in usr/local/bin,for example:

sudo rm python
sudo ln -s /usr/local/bin/python3.10 python

Check that this has worked using:

python –version.

If you are using VS Code then you can set the Python interpreter to use without having to reconfigure the system. All you have to do is click on the Python version displayed in the bottom right and set the path to the interpreter:

VSC6

After this VS Code will use the new version of Python by default.



Last Updated ( Tuesday, 07 May 2024 )