I have a python code that links C code into Python that I want to run. This code is inside the folder mymodule/foo which contains two files: a setup.py file and a foo.c file. What you usually had to do is to enter the mymodule/foo folder, and do
python setup.py build
This creates a .so file which you can then import in python. I have to do this for several sub-folders in the mymodule folder, so I wanted to do this automatically via an os.system() call. However, when I’m on the mymodule folder and do this:
os.system('python foo/setup.py build')
I get
gcc: error: foo.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
error: command 'gcc' failed with exit status 4
Apparently the setup.py file is trying to find the foo.c code in mymodule folder and not on the mymodule/foo folder. So far, my solution has been to copy the foo.c file into the mymodule folder, run that line of code, and then remove the copy of the file from mymodule folder. I find this solution, however, messy.
Do you have any ideas on how to do this elegantly?
Fix setup.py or chdir to the required directory using os.chdir().