2

I have a python "client.py" script as follows:

    import sys
    import traceback
    import client_new
    import subprocess
    def main():
        print "inside client"
        subprocess.Popen('C:/client_new.py',shell=True)
        #execfile('client_new.py')
    if __name__ == "__main__":
        is_sys_exit = False
        ret_val = 0
        try:
            ret_val = main()
        except SystemExit:
            is_sys_exit = True
        except:
            if not is_sys_exit:
                print( traceback.format_exc() )
                if sys.version_info < ( 3,0 ):
                    raw_input( "Press return to continue" )
                else:
                    input( "Press return to continue" )
                sys.exit( 1 )
        if is_sys_exit:
            print( "SystemExit Exception was caught." )
        sys.exit( ret_val )

The client_new.py script is as follows :

    import traceback
    def main():
        print "inside client new"

    if __name__ == "__main__":
        is_sys_exit = False
        ret_val = 0
        try:
            ret_val = main()
        except SystemExit:
            is_sys_exit = True
        except:
            if not is_sys_exit:
                print( traceback.format_exc() )
                if sys.version_info < ( 3,0 ):
                    raw_input( "Press return to continue" )
                else:
                    input( "Press return to continue" )
                sys.exit( 1 )
        if is_sys_exit:
            print( "SystemExit Exception was caught." )
        sys.exit( ret_val )

so, from client.py there is another script client_new.py being called using subprocess but when the client.py is executed it only prints its data and does not displays the print of client_new. Hence, i am not getting what am i doing wrong with the call of client_new.py. Please help what am i missing.

2
  • 3
    Why are you calling the other script in a subprocess, rather than simply importing and re-using its functionality? Commented Nov 9, 2016 at 12:25
  • @jonrsharpe actually i dont know how to do that could you please tell how Commented Nov 9, 2016 at 12:35

2 Answers 2

1

Providing they are both in the same directory (or they are in the directory that python looks for (I believe called PYTHONPATH)), it is relatively simple to do. To import a python file, just remove the .py and import. Therefore you just need the following code.

import client_new
#Write rest of the code here
client_new.run_function()

You will also need to change your code slightly in client_new so that it can work.

import traceback
def main():
    print "inside client new"

def run_function():
    is_sys_exit = False
    ret_val = 0
    try:
        ret_val = main()
    except SystemExit:
        is_sys_exit = True
    except:
        if not is_sys_exit:
            print( traceback.format_exc() )
            if sys.version_info < ( 3,0 ):
                raw_input( "Press return to continue" )
            else:
                input( "Press return to continue" )
            sys.exit( 1 )
    if is_sys_exit:
        print( "SystemExit Exception was caught." )
    sys.exit( ret_val )

if __name__ == "__main__": #This is only run if called as an external script.
    run_function()
Sign up to request clarification or add additional context in comments.

1 Comment

stackoverflow.com/questions/2349991/… might be helpful in addition...
1

If you have control over the client_new.py module, I'd strongly suggest A. N. Other's answer. If you don't, then change your main() function in client.py to:

def main():
    print 'inside client' 
    proc = subprocess.Popen('C:/client_new.py', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout = proc.stdout.read()
    stderr = proc.stdout.read()
    print 'Got output from client_new:\n' + stdout
    if stderr:
        print 'Got error output from client_new:\n' + stderr

Side note: don't use shell=True in subrocess if it can be avoided. Use subprocess.Popen(['client_new.py'], ...)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.