How do I get a Python script to execute another Python script from ArcMap?

1850
1
06-07-2012 04:26 PM
GuySerbin
New Contributor
Greetings,

I wrote a Python 2.6 script which calls another two Python scripts to help with my workflow.  They work great when I run them from Komodo IDE, but the adapted version of the main script fails to execute the other two scripts when run from ArcGIS 10.0.  The code is as follows:

import os

command = 'Y:/scripts/import_landsat_RGB.py' 
os.path.normcase(os.path.normpath(command))
outargs =command+' '+j[4]+' '+outpath
fi, foe = os.popen4(outargs) 


After viewing various posts here I tried this to no avail:

import os

command = 'Y:/scripts/import_landsat_RGB.py' 
os.path.normcase(os.path.normpath(command))
pyexe = 'D:/Python26/ArcGIS10.0/python.exe'
os.path.normcase(os.path.normpath(pyexe))
outargs =pyexe+' '+command+' '+j[4]+' '+outpath
fi, foe = os.popen4(outargs)


Anyone know what I am doing wrong?

Thanks,
Guy
Tags (2)
0 Kudos
1 Reply
MikeHunter
Occasional Contributor
Use subprocess:

    here = os.path.abspath('.')
    pyfile = os.path.join(here, 'StandDiaryODBC2.py')
    clst = [sys.executable, pyfile]
    DETACHED_PROCESS = 0x00000008
    p = subprocess.Popen(clst, shell=False, creationflags=DETACHED_PROCESS)


You may be able to get by without the creationflags arg, but you'll just have to see.  One cool thing about subprocess.Popen is that you pass it a list of commands and args, from which it correctly builds a command line (usually!). 

Mike
0 Kudos