简体   繁体   中英

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

How do you make it so that an already compiled python script that was turned into a.exe import a config file?

Here is what I have:

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

I didn't add the functions fconfStart() or Mainstart() because 1) they are really long and 2) they are not the problem because they aren't even called yet when I get an error saying "cannot import config"

fconfStart function creates the config.py file.

First time the script is run you create the configurations file then you close and reopen the program to load with the configuration file that is config.py

How the config file is created in the first time startup of the script. This is what happens when the confirm button is created (if it helps, I am using PyQt5 in this program):

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

Example Config.py

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

(If I need to use a different type of config file please let me know) This is what the script creates. Then when I reopen script to use this newly created config file I get the error:

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

Can anyone help me with this problem? Any help is greatly appreciated. If you need me to add something to help clarify the problem I will gladly do so.

When you convert a python script to.exe you take away the ability to dynamically load python files (plus it can cause silent errors).

In general if you want to save information permanently then you should use any type of file (for example.txt) but it is better to use a pre-established format (such as.ini, .yaml, .csv, etc) and use a library that read safely such as ConfigParser, QSettings, etc.

On the other hand you should not use getcwd() but you should obtain the information dynamically as the answers to this question suggest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM