Import PY file not included in py2exe executable

Florian Schmidt schmidt_florian at gmx.de
Thu Oct 4 20:04:32 CEST 2007


On Thu, Oct 04, 2007 at 10:15:59AM -0700, ward.david at comcast.net wrote:
> ... because they are used for program configuration ...

not sure if i completely understood but i guess you do something like
that:

my_config.py:
    db_host = "mydbserver"
    db_user = "root"
    ...

and in your program.py you have:
    import my_config
    ...
    DB.Connect(my_config.db_host, my_config.db_user...)

and your problem is that py2exe will package that my_config.py so that
"noone" can change it afterwards...

one possibility i often use is execfile:
same my_config as above, but:
program.py:
    class config_class:
          pass
    my_config = config_class()
    my_config.db_host = "localhost" # default config...

    def read_config():
        execfile("my_config.py", globals(), my_config.__dict__)
    ...
    DB.Connect(my_config.db_host, my_config.db_user...)      

so you can always call read_config() to re-read the configuration and have
all python features in that config file. (additionally you can catch
exceptions and check the config files' mtime if it has changed...)

that way py2exe won't care about your config file...

hope it helps...

-- 

Florian Schmidt



More information about the Python-list mailing list