如何在python中从另一个文件夹导入文件?

sqxo8psd  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(167)

如何从另一个文件夹导入文件,尝试了这么多的东西,但我的尝试没有成果。如何解决呢?

-> d:
   -> project Main
      -> First Folder
         -> my_main_file.py
            -> class (My_Main_Class)
               -> module1
               -> module2
               -> module3
      -> Second Folder 
         -> my_second_file.py

如何将模块1和模块3从我的第一个文件(my_main_file.py)导入my _second_file.py

My_main_file这是我的主文件('d:/project_main/main folder')。现在我想将模块“establish_connection_general和其他内容导入到我的第二个文件。(d:/project_main/second_folder)

from PyQt5.QtWidgets import QWidget,QMessageBox,QApplication
    from PyQt5.QtGui import QIcon

    import sqleet
    import os
    import sys
    print("1234567890aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    
    file_path_general = r"d:\project makeeasy\assist"
    db_name_general = (os.path.join(file_path_general,'makeeasy_general.me'))
    
    passkey_general = "1234"
    new_passkey_general = "1234"
    
    show_errormsg = "show"
    
    
    class Database_connection(QWidget):
    
        def __init__(self):
            super(). __init__()
            self.establish_connection_general()
            # self.cahnge_passkey()
    
        def establish_connection_general(self):
            try:
                connection_general = sqleet.connect(db_name_general,key = passkey_general)
                print("open scueffully")
            except Exception as e:
                if show_errormsg == "show":
                    self.handle_error(e)
                else:
                    pass
    
    
        def cahnge_passkey(self):
            try:
                general_connection = sqleet.connect(db_name_general,key = passkey_general)
                general_connection.change_key(new_passkey_general)
                print("new pass key sucessfully changed")
                general_connection.close()
    
            except Exception as e:
                if show_errormsg == "show":
                    self.handle_error(e)
                else:
                    pass
    
        def handle_error(self,error):
            exc_type, exc_value, exc_traceback = sys.exc_info()
            filenamewithpath = exc_traceback.tb_frame.f_code.co_filename
            head,tail = os.path.split((filenamewithpath))
            lineno = exc_traceback.tb_lineno
            name = exc_traceback.tb_frame.f_code.co_name
            type = exc_type.__name__
            message = exc_value
            nl = '\n'
            kk = f'File Name : {tail[:-3]}{nl}'\
                 f'Line No. : {lineno}{nl}'\
                 f'Type : {type}{nl}'\
                 f'Name : {name}{nl}'
            self.msg = QMessageBox()
            self.msg.setFixedSize(1600,400)
            self.msg.setWindowTitle("  Error/Bugs Information")
            self.msg.setWindowIcon(QIcon('icon\close006.png'))
            fd = "                                                                           "
            self.msg.setText(f'{type} - {lineno}{fd}')
            self.msg.setIcon(QMessageBox.Information)
            self.msg.setStandardButtons(QMessageBox.Ok)
            self.msg.setDefaultButton(QMessageBox.Ok)
            self.msg.setInformativeText("")
            self.msg.setDetailedText(kk)
            self.msg.show()
    
    def main():
        app = QApplication(sys.argv)
        ex = Database_connection()
        app.setStyle("Fusion")
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
ltskdhd1

ltskdhd11#

单位为my_second_file.py

from First_Folder.my_main_file import My_Main_Class

在您的评论之后:

from First_Folder.my_main_file import ex(your class object)

在你的第二个档案里

ex.module1()
ex.module3()

相关问题