通过在主模块1中运行功能将module2python文件导入到主模块1中的问题

问题描述

仅当我通过按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,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,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,QPushButton,QListView


class Window(QWidget):

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


app = QApplication(sys.argv)


def window():

    global widget
    widget = QWidget()
    widget.setGeometry(100,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,300)
    m_list.setStyleSheet("QListView{font-size: 12pt;}")
    m_list.setHidden(False)

    button2 = QPushButton(widget)
    button2.setText("Finish")
    button2.setGeometry(1100,600,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_())

欢迎任何帮助和支持!谢谢!

解决方法

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

但是您可以做的是:

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

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

,

尝试以下代码以获得一些提示:

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

以编程方式导入