【问题标题】:Problem with importing module2 (python file) into main module1 by running function in main module1通过在主模块1中运行函数将模块2(python文件)导入主模块1的问题
【发布时间】:2021-01-19 22:22:55
【问题描述】:

只有当我通过按下 main1 中的按钮调用函数时,我才需要从 module1 导入和运行脚本。但它不起作用。 如果我在 main1 模块的脚本顶部的 main1 模块中导入 module1,则导入有效。但我希望导入应该从函数发生。 我还需要在工作完成后卸载导入的 module1,程序应该返回到 main1 模块

两个模块(python 文件)都在同一个文件夹中,带有空的__init__.py 文件。


主模块代码


import sys

from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

# import module1                 #  This import code works, but I need it
# to work only when #import_but gets pressed


class Window(QWidget):

    def __init__(self):
        QWidget.__init__(self)

app = QApplication(sys.argv)


def window():

    global widget6
    widget6 = QWidget()
    widget6.setGeometry(100, 150, 1300, 800)
    widget6.setHidden(False)

    global start_frame
    start_frame = QFrame(widget6)
    start_frame.setGeometry(5, 5, 1300, 800)
    start_frame.setStyleSheet("QFrame {font-size: 13pt;}")
    start_frame.setHidden(False)

    global import_but
    import_but = QPushButton(start_frame)
    import_but.setGeometry(1050, 200, 200, 80)
    import_but.setHidden(False)
    import_but.setStyleSheet("QPushButton{font-size: 12pt;}")
    import_but.setText("Import Modul1")
    import_but.pressed.connect(import_but_pressed)


def import_but_pressed():  # This function does not work

    import modul1

    app.exec_()

# if __name__ == 'main1':
if __name__ == '__main__':
    window()
    sys.exit(app.exec_())


模块 1 的代码


import sys

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QListView


class Window(QWidget):

    def __init__(self):
        QWidget.__init__(self)


app = QApplication(sys.argv)


def window():

    global widget
    widget = QWidget()
    widget.setGeometry(100, 150, 1300, 800)
    widget.setHidden(False)

    w_list = QListView(widget)
    w_list.setStyleSheet("QListView{font-size: 12pt;}")
    w_list.setGeometry(30, 30, 250, 300)
    w_list.setHidden(False)

    m_list = QListView(widget)
    m_list.setGeometry(30, 400, 250, 300)
    m_list.setStyleSheet("QListView{font-size: 12pt;}")
    m_list.setHidden(False)

    button2 = QPushButton(widget)
    button2.setText("Finish")
    button2.setGeometry(1100, 600, 150, 150)
    button2.setStyleSheet("QPushButton{font-size: 12pt;}")
    button2.setHidden(False)

    def button2_pressed():

        widget.close()

     #  Here has to be placed  code for returning to main1.py

    button2.pressed.connect(button2_pressed)

    app.exec_()
if __name__ == 'modul1':
    # if __name__ == '__main__':
    window()
    sys.exit(app.exec_())

欢迎任何形式的帮助和支持!谢谢!

【问题讨论】:

    标签: python import python-module


    【解决方案1】:

    试试下面的代码以获得一点提示:

    import sys 
    
    def foo():
        import itertools #import module, for example the module itertools
        print([x for x in itertools.permutations(range(3))]) #arbitrary to show itertools imported
        del sys.modules['itertools'] #unload module by removing reference 
    
    def foo_without_del():
        import itertools
        print([x for x in itertools.permutations(range(3))])
    
    print('itertools' in sys.modules) #return False
    foo() #lazy import
    print('itertools' in sys.modules) #return False
    foo_without_del()
    print('itertools' in sys.modules) #return True
    

    卸载模块见unloading imported modules in python

    您也可以使用importlib.import_module以编程方式导入

    【讨论】:

      【解决方案2】:

      嗯,导入工作正常。发生的事情是您正在将模块导入函数 import_but_pressed 的本地范围。因此,一旦您退出函数,导入就会消失,因为您超出了范围。导入仅限于函数内部。

      但是你可以做的是:

      def import_but_pressed():
          globals()["modul1"] = __import__("modul1")
      

      这会将函数导入全局范围。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多