to launch an executable (.exe) from a python script

In Windows how do you launch an executable file from python? Internet search was not forthcoming on this for me, maybe others are looking for this as well.

I am starting to use C# for a lot of things and I want to launch my executables from my Blend environment.

import os
os.popen(“system command”) # that is any command from the OS shell - in your case ms-dos commands are available

#if you also want the messages returned by the system command:
f = os.popen(“system command”)
print f.read()

That’s cool!

but not quite portable and probably deprecated soon. If it works for you, it’s fine of course, but you might find usefull information in the docs for the subprocess module:

cheers

“If it works for you”

after playing around with it, I might be needing one more step to make it usable and practical.

While the screen that I launch is active …Blender is comatose.

That might work out where I just want to pull up a toolbox and sort out an answer, but if I want to use Blender I have to kill my other process.

Say for example I have a button in the python script that plays test.exe, or better yet C:test.mp3. Test .mp3 being some song that I am referencing.
It opens in it’s default application why should that need to tie up Blender?
Is there a workaround or a Pinvoke call that I should be using?

I guess I mean how do I open the app. as a process not a subprocess?
Until I looked at that documentation (Thanks Varkenvarken) I did not know about the “pipes”. Pipes look they would certainly be nice to utilize, but if a separate process did not have them it would usually not matter.

I tried spawn and popopen2 that’s not it…:frowning:

I believe you must create a thread so that the main program keeps running but lately I have no time so you’ll have to search for yourself.

Go to google and python documentation in the official site :wink:

CNC,

there is no need to run a seperate thread. This works for me (from the interactive python console inside blender2.49a:

import subprocess
pid=subprocess.Popen([“NOTEPAD.exe”]).pid

This starts the notepad editor as a separate process and returns The returned process id (pid) is a
handle that you can use to kill or wait on the process later if you want to.

cheers

PS> blender 2.49a uses python 2.6.x which means you can use the multiprocessing module as well.
http://docs.python.org/library/multiprocessing.html
Now that document is a bit daunting but underneath is a very straightforward interface (trust me :slight_smile: that is
simlar to threading. This is especially interesting if you want your processes to communicate in a simple and safe way.
But for simply running another process in the background the Popen() works just as well.

That works. Thanks

This is especially interesting if you want your processes to communicate in a simple and safe way.
Can “pickled” , serialized information in blender interact with marshalled information on the C# (.net) side then? How would that work?

Is that the “pipes” that I was reading about earlier in Python?
cnc

Depending on what you are doing, you may want to look into using IPC of some sort. Assuming your C# code is on windows, you might want to look into using COM.

In principle that would be possible: at the lowest level the pickle module marshalls its data as ascii, for example:

 
pickle.dump(1.0,stdout,0)
F1.0
.

(level 1 is binary, level 2 is suitable for new style classes).
Í am pretty sure the format is documented in detail but I couldn’t find quickly, Now, the C# (.net) side of the equation I can’t fill in since I have no idea whatsoever about the marshalling implementation of C# (.net)

Yes and no: the pipes are low level ‘connections’ between processes. The make it possible to pass anything to and from a child process, so even pickled data. Pipes are in many ways comparable to network connections between a client and a server. (and that’s very convenient if you want your workload distributed over more than one physical machine)

Depending again on what you need, FIshB8’s (D)COM suggestion might be useable. There are windows specific extensions available:

http://python.net/crew/mhammond/

and microsoft itself has examples available:

http://www.microsoft.com/technet/scriptcenter/scripts/python/os/com/default.mspx?mfr=true

There are even more possible solutions (just ideas, I haven’t tested them all ):

  • write out data as xml and parse it again: python (and I’m pretty sure C# (.net) as well) have libraries for dealing with xml. Two processes could communicate via pipes, via a network connection or even via a file,

  • use Remote Procedure Calls. That’s like the COM suggestion but a bit more general: your calls to another program are packaged as XML-over-http, the other program listens, unpacks the requests, performs the requested action and returns the result again. This is quite standards compliant, check the python docs for ‘RPC’, and for the C# (.net) ask around :slight_smile:

.

  • write out data as xml and parse it again: python (and I’m pretty sure C# (.net) as well) have libraries for dealing with xml. Two processes could communicate via pipes, via a network connection or even via a file,
    This is exactly what I have been stearing toward!

Let’s say there was a Wii controller or an Arduino board in the middle of all of this O.K.

I serialize the position data to blender after I “jazz it all up” in C# or C++ from the raw hardware data.

I’m a CNC machinist, not a programmer. so I am stumbling along a little bit blindly in “Managed”, “Marshalled”, “Pickled”, “serialized”,<CXML>, “Verse”, " Filepath:/CGI.Bin", “Python”, to Ironpython “.NET”, “Com Port” Nurbs Nodes.
You should see my girlfriend’s face “glaze over” when I talk sexy like that on a date.

However, If I can squirt data into Ghost windows fast enough and smoothly enough to supply UI navigation, then …well, Damn!

use Remote Procedure Calls. That’s like the COM suggestion but a bit more general: your calls to another program are packaged as XML-over-http, the other program listens, unpacks the requests, performs the requested action and returns the result again. This is quite standards compliant, check the python docs for ‘RPC’, and for the C# (.net) ask around :slight_smile:

Is a remote procedure call the python counterpart to C#'s Pinvoke?

sorry for the slow follow up: i was hospitalized for surgery on a herniated disk. It worked out ok, but the doctor more or less has forbidden me to sit behind a desk
for a while :smiley:

Anyway: I am not versed well enough in c# to say anything about Pinvoke (I think is a way to do procedure calls in other processes so more like RPC (remote procedure calls) but without the remote, but I can
give some short definitions for some of the strange python terms (some are not specific to python btw)

marshalling: converting data in a way that makes it possible tp transmit in a device independent way (so it can go over a network or be transferred to another process, xml might be used for this for instance)

pickling: typical python term. Making a persistent copy of some data structure. So pickling marshalls data, and does someting with it (typically dumping it to a file for later reuse)

Now basically if you can dump something to a file you can dump it to the standard input (stdin, or cin or whatever) just as easily: instead of opening a file and writing to it, you spawn a process en write to a pipe connected to its standard input. Any modern OS (including NT and XP) can do that.

hope this clears things up a bit.