Search code examples
pythonloadui

Trouble calling py script from another script


Im new to python, I created a py file from a ui file, the problem with that if i change something from the ui file nothing changes in py file therefore i made another py file that loads the ui file instead. With that if i change something in the ui file it also updates the py file. It is something like this.....

from PyQt5 import QtCore, QtGui, QtWidgets, uic

class Ui_DTR2(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_DTR2,self).__init__()
        uic.loadUi('dtr.ui',self)

if __name__=='__main__':
    import sys
    app=QtWidgets.QApplication(sys.argv)
    window=Ui_DTR2()
    window.show()
    sys.exit(app.exec_())

Now my problem is that how can i call the above py script from another py script?


Solution

  • By reference you have some very well structured answers here which itself is a duplicate of this, which might make yours a duplicate also :)

    In the .py script file simply import this one:

    #!/usr/bin/python
    import youpreviousfile    #without .py
    

    And the other example:

    test1.py

    def some_func():
        print 'in test 1, unproductive'
    
    if __name__ == '__main__':
        # test1.py executed as script
        # do something
        some_func()
    

    service.py

    import test1
    
    def service_func():
        print 'service func'
    
    if __name__ == '__main__':
        # service.py executed as script
        # do something
        service_func()
        test1.some_func()