简体   繁体   中英

Can I write my apps in python and then run them from C?

I need to write a client-server application. I want to write it in python, because I'm familiar with it, but I would like to know if the python code can be ran from C. I'm planning to have two C projects, one containing the server code, and one containing the client code.

Is it possible to eval the python code and run it ? Is there another way of doing this?

The bottom line is that the python code must run from C, and it must behave exactly as if ran under the python interpreter. I'm asking this now, because I don't want to waste time writing the python code just to find out later that I can't achieve this . As a sidenote, I only plan on using basic python modules ( socket,select, etc. ).

EDIT: maybe this edit is in order. I haven't embedded python in C before, and I don't know what the behaviour will be. The thing is, the server will have a select loop, and will therefore run "forever". Will C let me do that ?

EDIT2: here is why I need to do this. At school, a teacher asked us to do a pretty complex client-server app in C. I'm going to cheat, write the code in python and embed it in C.

这是一个很好的教程,可以准确地做到这一点http://www.linuxjournal.com/article/8497

It's called embedding Python -- it's well covered in the Python docs. See https://docs.python.org/extending/embedding.html

See how do i use python libraries in C++?

Yes you can run the Python code from C by embedding the interpreter in your program. You can expose portions of your C code to Python and call your exposed C code from Python as if they were normal Python functions.

A good start is the Embedding section in the Python docs. Also have a look at the article linked to by cobbal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM