如何从另一个python脚本调用python脚本?

时间:2016-11-27 22:02:08

标签: python import call

我在同一目录中有一个名为errorHandler.py的脚本和另一个名为test.py的脚本。我故意在test.py中的try语句中输入错误,我试图让errorHandler.py脚本捕获它。执行时它不会失败或产生错误,但errorHandler.py脚本也不会启动。任何帮助,将不胜感激。以下是两个脚本......

#--errorHandler.py

# python 3
try:
    from tkinter import *
    from tkinter import ttk
    from tkinter import messagebox
    import smtplib

# python 2.7
except:
    from Tkinter import *
    import ttk
    import tkMessageBox as messagebox
    import smtplib

class errorHandler:
    def __init__(self, master):

        master.title('Error')
        master.resizable(False, True)
        master.configure(background = 'cornsilk')
        self.mtr = master

        self.style = ttk.Style()
        self.style.configure('TFrame', background = 'cornsilk')
        self.style.configure('TButton', background = 'cornsilk')
        self.style.configure('TLabel', background = 'cornsilk', font = ('Arial', 11))
        self.style.configure('Header.TLabel', font = ('Arial', 18, 'bold'))

        self.frame_header = ttk.Frame(master)
        self.frame_header.pack()

        ttk.Label(self.frame_header, text = "ERROR", foreground = 'red', style = 'Header.TLabel', justify = 'center').grid(row = 0, column = 0, columnspan = 2)
        ttk.Label(self.frame_header, wraplength = 300,
              text = ("Please fill out the following form to help identify this error.  "
                      "Doing so will help make improvements to this program.")).grid(row = 1, column = 1)

        self.frame_content = ttk.Frame(master)
        self.frame_content.pack()

        ttk.Label(self.frame_content, text = 'Name:').grid(row = 0, column = 0, padx = 5, sticky = 'sw')
        ttk.Label(self.frame_content, text = 'Email:').grid(row = 0, column = 1, padx = 5, sticky = 'sw')
        ttk.Label(self.frame_content, text = 'Comments:').grid(row = 2, column = 0, padx = 5, sticky = 'sw')

        self.entry_name = ttk.Entry(self.frame_content, width = 24, font = ('Arial', 10))
        self.entry_email = ttk.Entry(self.frame_content, width = 24, font = ('Arial', 10))
        self.text_comments = Text(self.frame_content, width = 50, height = 10, font = ('Arial', 10))

        self.entry_name.grid(row = 1, column = 0, padx = 5)
        self.entry_email.grid(row = 1, column = 1, padx = 5)
        self.text_comments.grid(row = 3, column = 0, columnspan = 2, padx = 5)

        ttk.Button(self.frame_content, text = 'Send',
               command = self.send).grid(row = 4, column = 1, padx = 5, pady = 5, sticky = 'e')
        ttk.Button(self.frame_content, text = 'Clear',
               command = self.clear).grid(row = 4, column = 1, padx = 5, pady = 5, sticky = 'w')

        self.hdBtn = Button(self.frame_content, text='Hide Details', command = self.hideDetails)
        self.hdBtn.grid(row = 4, column = 0, padx = 5, pady = 5, sticky = 'w')
        self.hdBtn.grid_remove()

        self.swBtn = ttk.Button(self.frame_content, text='Show Details', command = self.showDetails)
        self.swBtn.grid(row = 4, column = 0, padx = 5, pady = 5, sticky = 'w')

        self.frame_Details = ttk.Frame(master)
        self.frame_Details.pack()

        self.text_comments_Det = Text(self.frame_Details, width = 50, height = 10, font = ('Arial', 10))
        self.text_comments_Det.grid(row = 3, column = 0, columnspan = 2, padx = 5)
        self.text_comments_Det.grid_remove()

    def send(self):
        msg = 'Name: \t\t%s\n\n' % self.entry_name.get() \
        +'Contact: \t\t%s\n\n' % self.entry_email.get() \
        +'Message: \t\t%s\n\n\n' % self.text_comments.get('1.0', END) \
        +'Error Message: \t%s' % self.err
        self.email(msg)
        messagebox.showinfo(title = 'Confirmation', message = 'Message Sent!')
        root.destroy()

    def clear(self):
        self.entry_name.delete(0, 'end')
        self.entry_email.delete(0, 'end')
        self.text_comments.delete(1.0, 'end')

    def showDetails(self):
        self.text_comments_Det.grid()
        self.hdBtn.grid()
        self.swBtn.grid_remove()
        self.mtr.geometry("368x520")
        self.text_comments_Det.delete('1.0', END)
        self.text_comments_Det.insert(END, self.err)

    def hideDetails(self):
        self.text_comments_Det.grid_remove()
        self.hdBtn.grid_remove()
        self.swBtn.grid()
        self.mtr.geometry("368x352")

    def err(self):
        try:
            import arcpy, sys, traceback
            # Get the traceback object
            tb = sys.exc_info()[2]
            tbinfo = traceback.format_tb(tb)[0]

            # Concatenate information together concerning the error into a message string
            errs = "\n\n\nPYTHON ERRORS:\n\tTraceback info:\n\t\t" + tbinfo + "\n\t" \
                + "Error Info:\n\t\t" + str(sys.exc_info()[1]) + "\n\n" \
                + 'GEOPROCESSING MESSAGE:\n\t' + arcpy.GetMessages(0) + "\n\n" \
                + 'GEOPROCESSING WARNING:\n\t' + arcpy.GetMessages(1) + "\n\n" \
                + 'GEOPROCESSING ERROR:\n\t' + arcpy.GetMessages(2) + "\n\n"
        except:
            import sys, traceback
            # Get the traceback object
            tb = sys.exc_info()[2]
            tbinfo = traceback.format_tb(tb)[0]

            # Concatenate information together concerning the error into a message string
            errs = "\n\n\nPYTHON ERRORS:\n\tTraceback info:\n\t\t" + tbinfo + "\n\t" \
                + "Error Info:\n\t\t" + str(sys.exc_info()[1]) + "\n\n"
        return errs

    def email(self, txt, toaddrs  = ['skeizer111@gmail.com']):
        fromaddr = 'yourerrorreports@gmail.com'
        msg = 'Subject: %s\n\n%s' % ("Error Report", txt)
        username = 'yourerrorreports@gmail.com'
        password = 'Alberta01'

        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login(username,password)
        for add in toaddrs:
            server.sendmail(fromaddr, add, msg)
            server.quit()

def runErrRpt():
    root = Tk()
    errorHandler(root)
    root.mainloop()

runErrRpt()




#--test.py
try:
    st = 1+"s"
    print st
except:
    import errorHandler

1 个答案:

答案 0 :(得分:0)

根据lmgtfy

  

获取一个python文件来运行另一个:

Put this in main.py: #!/usr/bin/python import yoursubfile.
Put this in yoursubfile.py #!/usr/bin/python print "hello";
Run it: python main. py.
It prints: hello.