I need to call a python script from another script, and not wait (this is a script to start multiple other scripts).
I have three different methods of calling the script - execfile(), os.system(), and subprocess.popen(). These all work - but, the first one forces the program to wait; and the other ones will fail when it tries to import arcpy. I suspect it is something to do with my 64-bit background geoprocessing, which I just reinstalled this morning. The error that occurs when importing arcpy is this:
"Traceback (most recent call last):
File "f:\scratch\test.py", line 5, in <module>
import arcpy
File "c:\program files (x86)\arcgis\desktop10.8\ArcPy\arcpy\__init__.py", line 22, in <module>
from arcpy.geoprocessing import gp
File "c:\program files (x86)\arcgis\desktop10.8\ArcPy\arcpy\geoprocessing\__init__.py", line 14, in <module>
from _base import *
File "c:\program files (x86)\arcgis\desktop10.8\ArcPy\arcpy\geoprocessing\_base.py", line 14, in <module>
import arcgisscripting
ImportError: DLL load failed: %1 is not a valid Win32 application."
I only have one installation of Python.
I'm attaching two scripts: test1.py will call test.py with the three different methods - if they are successful, they will write out text files indicating which method was used.
I can't attach my scripts here, so they are pasted below: The called script is commented out. I'd sure appreciate any help with this!
#MAIN SCRIPT
import os
import subprocess
import sys
#import arcpy #works whether or not this is commented
''' script that's called by 3 methods below:
#script f:\\scratch\\test.py
import sys
import time
outFile = sys.argv[1]
print (outFile)
import arcpy #if this is commented, all three methods work; otherwise only execfile works
print ("writing file")
newInputFile = "f:\\scratch\\" + outFile + ".txt"
inputfile = open(newInputFile, "w")
inputfile.close()
print(sys.version)
print("sleeping")
time.sleep(5)
'''
print ("execfile here:")
sys.argv = ['test.py','file_from_execfile']
execfile("f:\\scratch\\test.py")
print("")
command = 'python f:\\scratch\\test.py file_from_command'
print ("command here:")
os.system(command)
script = "f:\\scratch\\test.py"
pythonExePath = r'C:\Python27\ArcGISx6410.8\python.exe'
pid = subprocess.Popen([pythonExePath, script, 'file_from_subprocess'], shell=False) # Call subprocess
print ("done")
There are limits to 64 background geoprocessing. Most would move to ArcGIS Pro
Background Geoprocessing (64-bit)—ArcMap | Documentation (arcgis.com)