0

I need to import and run script from module1 only when I call function by pressing button in main1. But it does not work. Import works if I import module1 in main1 module in the top of the script for main1 module. But I want the import should happen from the function. Also I need the imported module1 should be unloaded after it's job is done and the program should return back to main1 module

Both modules (python files) are in the same folder with empty __init__.py file.


Code for main module


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_())


Code for module1


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_())

Any kind of help and support is welcome! Thank you!

Aryan
  • 1,093
  • 9
  • 22
Gvin47
  • 1
  • 1

2 Answers2

0

Well, the import is working as it should work . Whats happening is that you are importing the module into the local scope of the function import_but_pressed. Hence as soon as you exit the function the import vanishes because the you go out of scope. The import is limited to inside the function only.

However what you can do is:

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

This import the function into the global scope.

lefr0
  • 20
  • 5
0

Try the following piece of code for a little bit of a hint:

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

for unloading of modules, see unloading imported modules in python

You can also import programmatically using importlib.import_module

dswij
  • 148
  • 6