1

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.

Community
  • 1
  • 1
user1977981
  • 189
  • 1
  • 12

1 Answers1

2

OK i figured out what the problem was. The issue is not with the Eclipse IDE at all but it lies in my misunderstanding of how subprocess.Popen works. The subprocess.Popen class does not wait for subprocesses to finish. So because the function argums was calling further subprocesses the Popen class was not waiting for those subprocesses to finish hence script stalling at the top level process. If I replace:

 subprocess.Popen("python testpython.py --newscript 'this is the argument'", shell=True)

with either

 subprocess.Popen("python testpython.py --newscript 'this is the argument'", shell=True).wait()

or

 subprocess.call("python testpython.py --newscript 'this is the argument'", shell=True)

it now works. Hope this is of some help to someone else.

user1977981
  • 189
  • 1
  • 12
  • You don't need shell=true, pass a list of args and if you want to wait use call or check_call – Padraic Cunningham Dec 31 '15 at 19:37
  • Thanks Padraic that works also. The docs actually discourage the use of shell=true https://docs.python.org/2/library/subprocess.html#frequently-used-arguments as it means the script is vunerable from malicious shell injection attack. – user1977981 Jan 01 '16 at 10:17