How to execute foreing code from Python

Dave Angel davea at ieee.org
Sun Jan 23 19:24:36 EST 2011


On 01/-10/-28163 02:59 PM, hidura at gmail.com wrote:
> Hello i want to code from different languages using a Python script i
> know i can use os.system, but i don't know how to receive data or send
> arguments using that method if theres any another way to make it or
> there's a way to send arguments and receive data using os.system?
>
> Thanks in advance.
>

That sentence runs on, and makes no sense to me.  But I can guess that 
you would like to be able to write code in other languages, and call it 
from within a Python script.

If you're on Windows, and the other language is a compiled one like C, 
compile it into a DLL, and call that DLL from Python.  Ctypes is the 
first module to study.

If you're on Linux, and the code in the other language was written by 
someone else, and is already compiled into an executable you cannot 
change, you probably should invoke it using the subprocess module.  You 
can supply information to the executable via the commandline arguments 
and also via stdin, and you can receive results via the exit code, and 
via stdout.

Any more details and options would need lots more information about your 
environment and your constraints.

DaveA



More information about the Python-list mailing list