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!

2
  • 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. Commented Dec 7, 2010 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. Commented Dec 7, 2010 at 12:20

5 Answers 5

57

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"])
Sign up to request clarification or add additional context in comments.

2 Comments

What should args: a string with arguments? For example: args = "--option for my program"?
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.
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)

3 Comments

Anyone know where can I download 'cinpy'? The original URL is not available anymore.
OK, found it in Web Archive.
Although cinpy looks unmaintained, it's handy tool. The package fork: github.com/csarn/cinpy. The guide: amundblog.blogspot.com/2008/12/cinpy-or-c-in-python.html
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.

Comments

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)

3 Comments

You should probably prefer subprocess over os.system()
@tripleee why? I'm curious
@maynouf Because that's what the os.system documentation recommends. See also python.org/dev/peps/pep-0324 for a fuller rationale.
3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.