Pyinstaller在运行时python代码导入/加载,例如config.py



我有一个大型项目,该项目具有一个大的'config.py'文件,并变成了Pyinstaller的可执行文件。是否可以编译整个项目,但请在运行时离开/重载/导入此文件,以及如何?

(我可以将内容的一部分移至JSON格式,然后更改文件的逻辑,然后加载JSON文件,但不想加上它,它的大文件已经每个人都有自己的副本...(

" config.py"中的一小部分是:

import getpass
username = getpass.getuser()
data_files_dir = '/tmp/data'
bindings = 'UC'
if username.lower()=='nick':
   data_files_dir = '/tmp/data'
   ....
....

我发现的唯一相关问题是这个问题,但是它似乎是在要求更广泛的内容,再加上答案("您缺少这一点。适合我的案子...

ntg;

我发现与Pyinstaller一起使用的最佳解决方案是Python 3.4 Ementlib Module类SourceFileLeLoleLodeer,可在运行时加载配置信息。这是一个样本。

文件:./conf.py

"""
Configuration options
"""
OPT1='1'
OPT2='Option #2'
OPT3='The Lumberjack is ok'
OPT4='White over White. Have a nice flight'

接下来是一个示例脚本,将在运行时导入conf信息。

文件:myApp.py

#!/usr/bin/python3
"""
The Application.
"""
import sys
from importlib.machinery import SourceFileLoader
# load your config file here
conf = SourceFileLoader( 'conf', './conf.py' ).load_module()
print("Option 1:", conf.OPT1)
print("Option 2:", conf.OPT2)
print("Option 3:", conf.OPT3)
print("Option 4:", conf.OPT4)

请注意,来自conf.py的选项将导入到" conf"名称空间中。您需要将每个引用以" conf。"为前缀。可能还有其他方法。

相关内容

最新更新