Call C code in repository downloaded straight from GitHub

I write my programs in such a way that they can be downloaded straight from GitHub, requiring no packages to be installed. While that works for pure python code, I’m not sure how this works for C code.

Here’s what I would want to do:

  • Create a simple hello_world() in C
  • Call it from Python

That’s it.

If it’s easier, the function can simply return “Hello, world!” instead of printing it.

Is this is even possible?

No, this is not really possible in general for C. You will have to compile it (requiring a C compiler) and the code will potentially need to be modified to generate a library with the hello_world function being marked as a public function.

Outside of a toy example, what are you actually trying to do overall?

A conversion of a proprietary data format to json. Hundreds of folders containing each thousands of these files. My current Python code is far slower than other programs written in C.

So, if I want my code to be excutable, I need a Python version when it isn’t build, right?

You can include a C extension in your python project, and set up a backend that will build the extension on install. Users will need to have the proper tools installed on their machine (i.e. a compiler) but if they do they can install the package with pip install git+https://... and it will be built for them.

This can be a hassle for complex projects, and people generally prefer to download prebuilt wheels. But it works fine.

1 Like

I should add that I’ve primarily done this with Rust and maturin rather than C, but I believe it should work similarly.

Thanks. Note that that wouldn’t work when using git clone or when downloading the code straight from GitHub. But I think it would be a fun challenge to attempt.

I mean to make a project with C accelerators when building it with PyInstaller or installing it with pip.

It does require that the code is formatted as a package that can be installed [1].

I prefer this structure rather than a loose collection of scripts in a repo–it’s easier for me to grab the package on a new machine, and it allows me to import that code into other projects if appropriate. And I feel that organizing code into packages helps me to design things more cleanly. But other people do this differently.

  1. all that pip is doing in this situation is cloning the repo and running pip install . ↩︎

For reference, my current code structure looks like this: GitHub - nineteendo/pyvz2 at alpha

I have a for installing the dependencies if someone want to use them in their own scripts.
It might make sense to install as well such that the C implementation can be used.
Also, there shouldn’t be an in this directory, as it trips up linters.

You will want to have a read through: