python - Start external program without hardcoding the path -


i making gui supposed run on different computers. in script need open external program , run macro through program.i using subprocess so. problem without hardcoding path not able find program. cannot hardcode path, because program might in different directories on different computers. possible do?

code:

from subprocess import *  def call_dos(self, program, *args):      proc = call([program, args])     if proc:         logging.warning('calling failed')     else:         logging.info('calling successful')  def partone(self, *args):     try:         self.call_dos("myprogram.exe", r"c:\mymacro.mko")     finally:         self.partone() 

the error message:

traceback:     'calling failed' 

thank replies!

you have check myprogram.exe callable, i.e. in system path.

for windows, either set in user or system environment appending actual directory exe located path environment variable.

to make sure users don't blame program bad installation, add check error human-readable:

import distutils.spawn if not distutils.spawn.find_executable("myprogram.exe"):      raise exception("{} not in path, add path , retry".format("myprogram.exe")) 

you can distribute program alongside script (same directory), you're sure it's found (if doesn't need deep installation). in case, do:

self.call_dos(os.path.join(os.path.dirname(__file__),"myprogram.exe"), r"c:\mymacro.mko") 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -