如何从我的主脚本正确地运行/执行/从另一个目录导入另一个脚本?

时间:2019-10-22 13:34:09

标签: python pyqt pyqt5 python-import python-3.7

我想导入/执行另一个python文件以显示另一个目录中的另一个ui。我正在使用python3.7和pyqt5

如果我仅将其声明为印刷品。这行得通。 但是,当我将其与defFunc连接时,第二个脚本窗口不会出现。

将其与defFunc连接后。第二个窗口不会出现。 为什么以及如何解决?

我已经使用导入方式,但是不会出现另一个窗口。 即使使用了某些exec也无法正常工作。 我使用了几种方法。

我的文件夹如何管理:

myFolder
|
|---__init__.py
|
|---main
|    |--__init__.py
|    |--main.py
|    
|---scripts
|    |--__init__.py
|    |
|    |--js
|    |
|    |--python
|         |--__init__.py
|         |
|         |--iWantToCallThis.py
|----src
     |--__init__.py
     |
     |--"I put icon, images and something here. Which still works fine."

main.py

import os, sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic


OpDf = os.path.dirname( __file__ )


class main(QtWidgets.QMainWindow):
    def setupUi(self, MainWindow):
        #setup ui here
        self.show()

        #and another one and another one
        #then

        self.accessButton.clicked.connect(self.__myAccesFunc)

    def __myAccesFunc(self):
#--1st attempt--# Failed
        import scripts.python.prPM_adminLogin as sppal

        sppal()

#--2nd attempt--# Failed. execfile also failed.
        exec (OpDf + "../../scripts/python/prPM_adminLogin.py")

#--3rd attempt--# Failed  
        from scripts.python import prPM_adminLogin 

#--4th attempt--# <--- I dont get this one.
        import importlib.util
        spec = importlib.util.spec_from_file_location("scripts", "/scripts/python/prPM_adminLogin.py")
        spcs = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(spcs)
        spcs.admin_ui_form()

        import importlib.util
        spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")
        foo = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(foo)
        foo.MyClass()
#--5th attempt--# Failed
        #using __init__.py and implement it and typed on it with > from something import anotherThing

#--6th attempt--# Failed
        from .scripts.python import prPM_adminLogin as prpmal
        prpmal.zulu()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = main()
    ui.setupUi(MainWindow)
    #MainWindow.show()
    print ( "Hi, this is from main. Good luck!" )
    sys.exit(app.exec_())

所以这是我要导入并运行的东西

iWantToCallThis.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class iWantToCallThis(QtWidgets.QMainWindow):
    def setupUi(self, Form):
        #yes and another and another one to setup ui

        #another method yada yada

    def anotherDefFunc(self):
        #some condt applied

def zulu():
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Form = QtWidgets.QWidget()
        ui = iWantToCallThis()
        ui.setupUi(Form)
        Form.show()
        sys.exit(app.exec_())

zulu()


两个脚本仍在单个脚本中运行

这里是来自vscode输出的错误消息:

#-----------------------------------------------------#

#[Running] python -u "d:\_prsfx\testAndDebug\app\main.py"
#Hi, this is from main. Good luck!

#[Done] exited with code=3221226505 in 2.334 seconds

#-----------------------------------------------------#

0 个答案:

没有答案