如何使已编译的 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) 它们不是问题,因为当我收到错误消息“无法导入配置”时,它们甚至还没有被调用”
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 的答案。建议。
关于python - 从编译的 python .exe 脚本中读取 config.py 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61416629/