从已编译的 python .exe 脚本中读取 config.py 文件

Read config.py file from a compiled python .exe script

如何才能将已编译的 python 脚本转换为 .exe 导入配置文件?

这是我的:

#Rest of code that show the display and options are above ^

if __name__ == "__main__":

    cwd = os.getcwd()
    variableCheck = Path(cwd + '/config.py')
    print(variableCheck)
    print(cwd)
    variableCheck.is_file()
    if variableCheck.is_file():
        from config import *

        #Next print uses variables from config

        print("ssh = " + ssh + "\nftp = " + ftp + "\nproftpd = " + proftpd + "\nvsftpd = " + vsftpd + "\nweb = " + web + "\napaweb = " + apaweb + "\nnginweb = " + nginweb + "\nhttps = " + https + "\nsmb = " + smb + "\nsql = " + sql + "\nrsnc = " + rsnc)

        print('Configuration file has been loaded...')

        app = QApplication(sys.argv)
        main = Mainstart()
        main.show()
        sys.exit(app.exec_())
    else:
        print('Ello, you have some configurations to do!')
        app = QApplication(sys.argv)
        main = fconfStart()
        main.show()
        sys.exit(app.exec_())

我没有添加 fconfStart() 或 Mainstart() 函数,因为 1) 它们真的很长 2) 它们不是问题,因为当我收到错误提示 "cannot import config"

fconfStart 函数创建 config.py 文件。

第一次脚本是 运行 创建配置文件然后关闭并重新打开程序以加载配置文件 config.py

脚本第一次启动时如何创建配置文件。 这是创建确认按钮时发生的情况(如果有帮助,我在此程序中使用的是 PyQt5):

#Rest of configuration options that users answer are above this piece of code ^

 def confirmBTTN():
            if self.ssh != '' and self.ftp != '' and self.proftpd != '' and self.vsftpd != '' and self.web != '' and self.apaweb != '' and self.nginweb != '' and self.https != '' and self.smb != '' and self.sql != '' and self.rsnc != '':
                print('saving configurations\n')
                print("ssh=" + self.ssh + ", ftp=" + self.ftp + ", proftpd=" + self.proftpd + ", vsftpd=" + self.vsftpd + ", web=" + self.web + ", apaweb=" + self.apaweb + ", nginweb=" + self.nginweb + ", https=" + self.https + ", smb=" + self.smb + ", sql=" + self.sql + ", rsnc=" + self.rsnc)
                f = open("./config.py", "a+")
                f.write("ssh = " + '"{}"'.format(self.ssh) + "\nftp = " + '"{}"'.format(self.ftp) + "\nproftpd = " + '"{}"'.format(self.proftpd) + "\nvsftpd = " + '"{}"'.format(self.vsftpd) + "\nweb = " + '"{}"'.format(self.web) + "\napaweb = " + '"{}"'.format(self.apaweb) + "\nnginweb = " + '"{}"'.format(self.nginweb) + "\nhttps = " + '"{}"'.format(self.https) + "\nsmb = " + '"{}"'.format(self.smb) + "\nsql = " + '"{}"'.format(self.sql) + "\nrsnc = " + '"{}"'.format(self.rsnc))
                f.close()

                RESTART = QMessageBox()
                RESTART.setWindowTitle("Hey! Listen!")
                RESTART.setText("Reopen the program to continue.")
                RESTART.setIcon(QMessageBox.Information)
                RESTART.setWindowIcon(QtGui.QIcon('HEY.png'))
                RESTART.setStandardButtons(QMessageBox.Close)
                RESTART.buttonClicked.connect(lambda: sys.exit(0))
                x = RESTART.exec_()
            else:
                HEY = QMessageBox()
                HEY.setWindowTitle('Hey! Listen!')
                HEY.setText("Hey! You have not finished filling in all of the choices!")
                HEY.setIcon(QMessageBox.Critical)
                HEY.setWindowIcon(QtGui.QIcon('HEY.png'))
                x = HEY.exec_()

示例Config.py

ssh = "yes"
ftp = "yes"
proftpd = "yes"
vsftpd = "no"
web = "yes"
apaweb = "yes"
nginweb = "no"
https = "yes"
smb = "yes"
sql = "yes"
rsnc = "no"

(如果我需要使用不同类型的配置文件,请告诉我) 这就是脚本创建的内容。然后,当我重新打开脚本以使用这个新创建的配置文件时,出现错误:

Traceback (most recent call last):
  File "ScriptGUIrunner.py", line 380, in <module>
    from config import *
ModuleNotFoundError: No module named 'config'
[20724] Failed to execute script ScriptGUIrunner

谁能帮我解决这个问题? 任何帮助是极大的赞赏! 如果您需要我添加一些内容来帮助澄清问题,我很乐意这样做。

当您将 python 脚本转换为 .exe 时,您将失去动态加载 python 文件的能力(而且它可能会导致静默错误)。

一般来说,如果您想永久保存信息,那么您应该使用任何类型的文件(例如 .txt),但最好使用预先建立的格式(例如 .ini、.yaml、.csv)等)并使用安全读取的库,例如 ConfigParser、QSettings 等

另一方面,您不应该使用 getcwd(),但您应该按照 this question 的建议动态获取信息。