Monday, January 29, 2024

[SOLVED] Include compiled C file into Python

Issue

I got a compiled C file (let's say testprog) from a person who is not here anymore, which takes 2 parameters when calling. They way I call the file from the unix shell is

testprog arg1 arg2

where arg1 is a file.

I am creating a GUI using python that generates the arg1 file easily instead of writing it manually. Now my questions are:

  1. How do I integrate the compiled C file into my python code, so that I can pass the unix command via python and get the results/output directly?
  2. How do I compile everything together (with the compiled C file) so that I can distribute my GUI to other people?

The final target will be providing a single file to users that incorporates my python gui and the compiled C


Solution

For your 1st problem I reccomend you checkout the subprocess package.

import subprocess

output = subprocess.check_output(["testprog", "qrg1", "arg2"])

or the ctypes package which allows you to call c functions from python. For the second problem, I think you will need to send both the python and compiled c for both of my solutions.

edit: Maybe checkout py_compile



Answered By - potatoOnABus
Answer Checked By - Marilyn (WPSolving Volunteer)