Really perplexed why this is not working. I have a python script which runs another python script as a subprocess using the subprocess module:
##### outerscript.py
import subprocess
subprocess.Popen("python testpython.py --newscript 'this is the argument'", shell=True)
The python script being run as a subprocess looks like this:
###### innerscript.py
import getopt
class Checkclass(object):
def __init__(self):
self.newscript = ''
def check(self):
print self.newscript
def complicatedcalculation(self):
print 1+1
def argums(argv):
checkclass = Checkclass()
checkclass.check()
checkclass.complicatedcalculation()
#Generates and interprets a list of options which can be passed as arguments to the class Checkclass
try:
opts, args = getopt.getopt(sys.argv[1:], "h", ["newscript="])
except getopt.GetoptError, err:
print str(err)
checkclass.usage()
sys.exit(2)
output = None
verbose = False
for o, a in opts:
if o in ("-h", "--help"):
#usage function not shown for simplicity-sake
checkclass.usage()
sys.exit()
elif o == "--newscript":
if a in ("-h", "--help", "--livescript"):
print "need to define --newscript"
checkclass.usage()
sys.exit()
else:
checkclass.newscript = a
checkclass.check()
if __name__ == "__main__":
argums(sys.argv[1:])
When I run this script it generates an error stating
Traceback (most recent call last): File "innerscript.py", line 45, in
Line 45 relates to the first line of the script to be executed i.e.
argums(sys.argv[1:])
The error doesn't seem to be that informative. I have been trying to come up with a solution for a while. I have tried running other more simpler python scripts through as subprocesses and they seem to work fine. I also can run this exact subprocess.Popen command on the terminal with no issue so it suggests it relates to the Eclipse IDE environment. When I look at sys.path on my terminal and IDE they are identical except that the Eclipse IDE also includes the paths relating to my Eclipse project folders. I have seen others post issues regarding issues with Eclipse and subprocess subprocess.Popen() has inconsistent behavior between Eclipse/PyCharm and terminal execution but the issues and answer have not got me to a solution, but maybe it will help others with similar issues.