22

I have this C program, at least I think it is (files: spa.c, spa.h). Is there any way I can execute this script from Python WITHOUT passing extra arguments to the Python interpreter (if not, what would the arguments be?)

Update: Thanks for your replies. The source code can be found at http://www.nrel.gov/midc/spa/#register

(Please do not be scared by the 'register' in the url, if you fill in the form, you can immediately download the files (no validation mails, etc) I will try your suggestions and report back with the results.

Update 2: I compiled the source code using gcc, but now it gives me a permission denied when trying to call(), even when running python as root (im on Ubuntu 10:10).

Update 3 [Errno 8] Exec format error

Update 4 Ok, I got it working. Program outputs values using printf:

>>> call(['/path'])
Julian Day:    2452930.312847
L:             2.401826e+01 degrees
B:             -1.011219e-04 degrees
R:             0.996542 AU
H:             11.105902 degrees
Delta Psi:     -3.998404e-03 degrees
Delta Epsilon: 1.666568e-03 degrees
Epsilon:       23.440465 degrees
Zenith:        50.111622 degrees
Azimuth:       194.340241 degrees
Incidence:     25.187000 degrees
Sunrise:       06:12:43 Local Time
Sunset:        17:20:19 Local Time

Thanks all!

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
Izz ad-Din Ruhulessin
  • 5,955
  • 7
  • 28
  • 28
  • Could you post here some excerpts from your `spa.c` (or, better yet, entire code)? Though some C-like scripting implementations do exist, normally C is a compiled language. Even if you find some C interpreter written in Python, chances of getting an arbitrary C program work in it are very slim. – atzz Dec 07 '10 at 12:12
  • As far as I know, there are no any arguments that can convert Python interpreter to C compiler. But you can use python ctypes to run C code from dlls or shared libs. – Mikhail Churbanov Dec 07 '10 at 12:20

5 Answers5

55

There is no such thing as a C script. If you meant a C program you need to compile spa.c and spa.h into an executable before running it.

If you use GCC in Linux or Mac OS X:

$ gcc -Wall spa.c -o spa

Will get you an executable named spa.

After that, you can run spa program from your Python script with:

from subprocess import call
call(["./spa", "args", "to", "spa"])
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • What should `args`: a string with arguments? For example: `args = "--option for my program"`? – Sigur Nov 13 '17 at 22:52
  • Then `call(["your program", "--option", "for", "my", "program"])` if indeed you want the option to be multiple arguments. Perhaps you mean `call(["your program", "--option", "for my program"])` but only you can know that. – tripleee Jan 29 '19 at 09:58
14

cinpy comes close using the awesome combination of tcc and ctypes

The following code is ripped from cinpy_test.py included in the package.

import ctypes
import cinpy

# Fibonacci in Python
def fibpy(x):
    if x<=1: return 1
    return fibpy(x-1)+fibpy(x-2)

# Fibonacci in C
fibc=cinpy.defc("fib",
                ctypes.CFUNCTYPE(ctypes.c_long,ctypes.c_int),
                """
                long fib(int x) {
                    if (x<=1) return 1;
                    return fib(x-1)+fib(x-2);
                }
                """)

# ...and then just use them...
# (there _is_ a difference in the performance)
print fibpy(30)
print fibc(30)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • Anyone know where can I download 'cinpy'? The original URL is not available anymore. – OGP Jul 23 '15 at 06:03
  • OK, found it in Web Archive. – OGP Jul 23 '15 at 07:55
  • Although cinpy looks unmaintained, it's handy tool. The package fork: https://github.com/csarn/cinpy. The guide: http://amundblog.blogspot.com/2008/12/cinpy-or-c-in-python.html – Ivy Growing Sep 30 '18 at 09:18
5

C is not a scripting language. You have to compile spa.c into an executable. You don't say your OS, but if Mac or Linux, try

  gcc spa.c -o spa

If that works, you now have a executable named spa. You can use python's os.system() to call it.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
4

there is no such thing as a C script you need to compile C programs... if you compile to an executable, you can execute it using os.system(CommandLine)

tobyodavies
  • 27,347
  • 5
  • 42
  • 57
2

Question is old still I felt one important part is missing!

C code can be compiled AND executed within Python script:

#include <iostream>
using namespace std;
int main(){
cout<<"Hello World\n";
return 1;
}



import subprocess
subprocess.call(["g++", "hello_world.cpp"]) # OR gcc for c program
tmp=subprocess.call("./a.out")
print("printing result")
print(tmp)


$ python main.py 
Hello World
printing result
1

I referred to the post on Quora: https://www.quora.com/How-do-I-write-a-Python-script-to-run-a-C-program

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shantanu pathak
  • 2,018
  • 19
  • 26