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.

Learner
  • 453
  • 13
  • 29

2 Answers2

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()
A. N. Other
  • 392
  • 4
  • 14
  • http://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files might be helpful in addition... – A. N. Other Nov 09 '16 at 12:42
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'], ...)

Billy
  • 5,179
  • 2
  • 27
  • 53