编译代码中的可修改配置文件? [英] modifiable config files in compiled code?
问题描述
大家好,
我一直在尝试为这个问题找到一个优雅的解决方案,
但似乎无法想想比我下面的解决方案更好的东西。
我有一个需要转换成可执行文件的Python程序。
问题是我有一个"配置"我曾经用过的模板文件
用于修改路径,全局
变量,结构等初始化常量。显然,一旦我将代码转换为
可执行文件我无法再读取/写入配置文件。
我的解决方案是将此配置文件转换为标准ASCII文本
文档并让Python为用户集变量解析它。这不是
看起来像是最优雅的解决方案;然而,我认为其他人
必须实现比这更好的方式。
任何人都有更好的解决方案来解决这个问题吗?
Jay
Hi All,
I''ve been trying to come up with an elegant solution to this problem,
but can''t seem to think of anything better than my solution below.
I have a Python program that needs to be converted into an executable.
The problem is that I have a "config" template file that I''ve been
using to modify initialization constants such as paths, global
variables, structures, etc. Obviously, once I convert my code into an
executable I can no longer read/write to my configuration file.
My solution was to convert this config file into a standard ASCII text
document and have Python parse it for user set variables. This doesn''t
seem like the most elegant of solutions; however, and I thought others
must have implemented a better way than this.
Anyone have a better solution for this problem?
Jay
推荐答案
2005年3月10日06:02:22 -0800, ga ******** @ gmail.com < ga ******** @ gmail.com>写道:
10 Mar 2005 06:02:22 -0800, ga********@gmail.com <ga********@gmail.com> wrote:
大家好,
我一直试图想出一个优雅的解决方案来解决这个问题,
但似乎无法思考什么比我的解决方案更好。
我有一个需要转换成可执行文件的Python程序。
问题是我有一个配置我一直用来修改路径,全局变量,结构等初始化常量的模板文件。显然,一旦我将代码转换成可执行文件,我就不能再这样了读取/写入我的配置文件。
我的解决方案是将此配置文件转换为标准的ASCII文本文档,并让Python为用户设置变量解析它。这似乎不是最优雅的解决方案;然而,我认为其他人必须实现比这更好的方式。
任何人都有更好的解决方案来解决这个问题吗?
杰伊
-
http://mail.python .org / mailman / listinfo / python-list
不知道你是否知道这个......
#example
来自ConfigParser import ConfigParser
def getconfig():
""
初始化配置设置
"""
result = ConfigParser()
结果。 read(" config.ini")
返回结果
def main():
"""
运行应用程序
""
config = getconfig()
....
dbsettings = {}
for key,config.items(" db")中的值:
print&q uot;%s:%s" %(键,值)
#etc ...
#------------------ -config.ini --------------
; db settings
[db]
server = 127.0.0.1
db = db
user = user
密码=密码
#----- ---------------结束config.ini ------------
还有其他项目需要处理配置信息但是
我认为包含了pyig的ConfigParser。
希望有帮助...
-
Thomas G. Willis
http:// paperbackmusic .net
注意:我的评论假设Windows发行版。
为什么你认为你不能将程序转换为可执行文件后,你有一个配置文件吗?我一直这样做,所以很多
其他程序。 .INI配置文件只是一个单独的文件,
提供了一种将客户端提供的信息传递到
可执行文件的好方法(我假设当你说可执行文件时)意思是
通过像py2exe这样的程序。您也可以将
信息作为参数传递给您的程序,或者我通常会将两者的组合传递给
。
我采取的步骤:
1)通过py2exe运行程序
2)构建一个Inno Installer脚本来收集我所有的程序部分
和我的.INI和README.TEXT等文件一起组成一个
setup.exe。这可能包括数据文件或其他内容......额外的额外内容。该程序需要的文件。
3)插入Inno Installer命令,使注册表或我的.INI文件所需的任何安装时间更改
。
4)让Inno Installer将所有内容编译成setup.exe
viola''你有一个文件可以安装在任何计算机上
将运行没有Python安装。
-Larry Bates
ga **** **** @gmail.com 写道:
Note: my comments assume Windows distribution.
Why do you think you can''t you have a config file after you convert
your program to an executable? I do it all the time and so do many
other programs. The .INI config file is just a separate file that
provides a good way to pass client supplied information into an
executable (I''m assuming when you say executable you mean something
that goes through a program like py2exe). You can also pass in
information as arguments to your program or as I normally do some
combination of the two.
Steps I take:
1) Run program through py2exe
2) Build an Inno Installer script to gather all my program parts
and my .INI and README.TEXT, etc. files together into a single
setup.exe. This may include things like data files or other
"extra" files that the program requires.
3) Insert Inno Installer commands to make any install-time
changes that are required to registry or my .INI file.
4) Have Inno Installer compile everything together into setup.exe
viola'' you have a single file that can be installed on any computer
that will run without Python installation.
-Larry Bates
ga********@gmail.com wrote:
大家好,
我一直试图想出一个优雅的解决方案来解决这个问题,
但似乎没有比我下面的解决方案更好的想法。
我有一个需要转换成可执行文件的Python程序。
问题是我有一个配置我一直用来修改路径,全局变量,结构等初始化常量的模板文件。显然,一旦我将代码转换成可执行文件,我就不能再这样了读取/写入我的配置文件。
我的解决方案是将此配置文件转换为标准的ASCII文本文档,并让Python为用户设置变量解析它。这似乎不是最优雅的解决方案;然而,我认为其他人必须实现比这更好的方式。
任何人都有更好的解决方案来解决这个问题吗?
Jay
Hi All,
I''ve been trying to come up with an elegant solution to this problem,
but can''t seem to think of anything better than my solution below.
I have a Python program that needs to be converted into an executable.
The problem is that I have a "config" template file that I''ve been
using to modify initialization constants such as paths, global
variables, structures, etc. Obviously, once I convert my code into an
executable I can no longer read/write to my configuration file.
My solution was to convert this config file into a standard ASCII text
document and have Python parse it for user set variables. This doesn''t
seem like the most elegant of solutions; however, and I thought others
must have implemented a better way than this.
Anyone have a better solution for this problem?
Jay
Larry Bates写道:
Larry Bates wrote:
注意:我的评论假设Windows发布。
为什么你认为你可以''在将程序转换为可执行文件后,您有配置文件吗?我一直这样做,所以很多
Note: my comments assume Windows distribution.
Why do you think you can''t you have a config file after you convert
your program to an executable? I do it all the time and so do many
我怀疑OP的配置文件是一个Python模块。
问候
史蒂夫
I suspect the OP''s config file is a Python module.
regards
Steve
这篇关于编译代码中的可修改配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!