1

I wrote a wxPython GUI where I currently configure some of the widgets and some default values by using "import data" for a module file containing several lists.

But I need to compile the whole program using py2exe for a user without a python installation. In so doing, I lose the capability of letting the user edit the data.py file to change those configuration defaults.

I could put each list as a series of text strings and read and parse the whole thing, but that seems like a huge waste when python already can do all that by a simple import statement.

I could probably do it with xrc/xml or perhaps ConfigParser but it would seem there should be an easy way to sort of import data.txt

or something similar and let python do it's thing! Then when py2exe gets hold of it it, it wouldn't create un-editable byte-code for the data.txt file.

Any suggestions?

kmceng
  • 65
  • 4
  • It appears the 'exec' module is just what I was looking for. I could leave the list statements in a text file, read the file as a string, and then exec(string) to get the lists I wanted. (and this AFTER I already implemented in ugly xml--oh well) – kmceng Nov 28 '11 at 18:42
  • exec is not a module, it is a keyword/statement and in python 3, a function. Be careful with it, just check [this](http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice) – joaquin Nov 30 '11 at 19:10

1 Answers1

0

files that are imported are bundled in the executable by py2exe. The way to go is to use a configuration file that you package with your executable in a zip or with Inno Setup. Configuration files are files made to be changed at some moment, contrarily a user should not be modifying a python script. I tell you because some 'negative' experiences to say something polite.

For my programs (practically all use wxPython GUIs) I use to have a py module with configuration data (directories, etc) and some globals. This module is used to load default parameters if the program does not find my .ini file or if that especific parameter has not been set in the ini. Then I distribute everything (exe and auxiliary files) with inno setup.

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • OK, but how does one "import" a config.ini file? When I import a .py file each var_name = ['a', 'b', 'cdata', 67 .....] comes in nicely as a python list, without having to parse text strings and such. (security not really an issue since end-users are internal) – kmceng Nov 26 '11 at 20:16
  • 1
    if you want to load/import something more estructured, in the form of lists and dictionaries but still being capable of manually reading and writing, one powerful solution is to use a JSON file. The [ConfigObj](http://www.voidspace.org.uk/python/configobj.html#introduction) library is and alternative to the python oficial parser and also allows reading lists of values. – joaquin Nov 26 '11 at 21:17