导出到EXE后更新数据库

时间:2018-08-20 12:42:16

标签: python dictionary exe pyinstaller

我正在导出用Python创建的脚本,该脚本将命令发送到投影仪的IP地址以将其关闭。该代码的功能正常,投影机将关闭。投影机列表存储在字典中与脚本不同的文件中,以便其他脚本可以对其进行编辑和访问。

使用针对Python 3.5.1的Pyinstaller v3.3.1将脚本导出到exe后,.exe文件不再从包含字典的.py文件更新,而是已经在内存中存储了一个版本无法更新。

如何使可执行文件仍从词典中读取并在每次运行时进行更新?

谢谢, 乔什

代码: dictonaryfile.py(出于安全考虑而减少,但显示了格式)。

projectors = {
    '1': '192.168.0.1'
}

执行关机的脚本

from pypjlink import Projector
from file3 import projectors

for item in projectors:
    try:
        myProjector = Projector.from_address(projectors[item])
        myProjector.authenticate('PASSWORD REMOVED FOR SECURITY')
        state = myProjector.get_power()
        try:
            if state == 'on':
                myProjector.set_power('off')
                print('Successfully powered off: ', item)
        except:
            print('The projector in ', item, ', raised an error, shutdown 
may not have occurred')
            continue
    except:
        print(item, 'did not respond to state request, it is likely powered 
off at the wall.')
        continue

1 个答案:

答案 0 :(得分:0)

您注意到,一旦制作了exe,就无法更新它。此类问题的解决方法是询问代码中dictonaryfile.py的位置-

from pypjlink import Projector

projector_location = input('Enter projector file location')
with open(projector_location) as f:
    for line in f:
        # extract data from f
        ....

对于此类应用程序,最好获取一个配置文件(.ini),而python具有Configparser可以从配置文件中读取。您可以将配置文件创建为-

[Projectors]
1 = '192.168.0.1'
2 = '192.168.0.45' # ..and so on

并使用Configparser-

从此文件中读取投影仪
config = configparser.ConfigParser()
config.read(projector_location)
projectors = config['PROJECTORS']
for k, v in projectors.items():
    # your code here