Python and py2exe - Implicitely Importing Modules
Question: I've used py2exe several times in the past to create *.exe files for my python
programs. However, I'm getting an error this time. I think I know what the
issue is, but I don't know how to resolve it.

I have a handful of wx.Panels in a subfolder and it could be a variable
amount, so I import them via a function that finds the *.py files in the
folder and calls the function below to actually import each panel.

In normal python, this works well. However, py2exe leaves these files out. I
assume that because they are not explicitly imported, py2exe doesn't believe
they are needed. Is there a solution to this? Some option in py2exe that I'm
unaware of?

Thanks!

    
    
    # module = Module to be imported (string)
    # folder = Folder containing the module (string)
    def import_module(module, folder=None):
       if folder is None:
          return __import__(module)
       return getattr(__import__('%s.%s' % (folder.replace(os.path.sep, '.'),
          module)), module)
    
    
    ...within some other function...
    modules = [import_module(os.path.basename(os.path.splitext(filename)[0]), 'Panels') for filename in glob.glob('Panels//*.py')]
    

**EDIT**

I'm adding a sample setup.py script I've used. But I've used probably 20
different variations and several completely different scripts (what I could
find on the internet). Note that one requirement is that it is completely
self-contained in one executable file.

    
    
    from distutils.core import setup
    import py2exe
    
    import wxversion
    wxversion.select("2.8.12.1")
    import wx
    import wx.lib.pubsub
    
    includes = []
    excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
                'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
                'Tkconstants', 'Tkinter']
    packages = ['wx.lib.pubsub']
    dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
                    'tk84.dll']
    
    import glob
    my_data_files = [('Panels', glob.glob('Panels/*.py'))]
    
    setup(
        options = {"py2exe": {"compressed": 2,
                              "optimize": 2,
                              "includes": includes,
                              "excludes": excludes,
                              "packages": packages,
                              "dll_excludes": dll_excludes,
                              "bundle_files": 2,
                              "dist_dir": "dist",
                              "xref": False,
                              "skip_archive": False,
                              "ascii": False,
                              "custom_boot_script": '',
                             }
                  },
        zipfile = None,
        #data_files = my_data_files,
        windows=['Main.py']
    )
    


Answer: I believe I've found the solution for my issue. In my setup.py file, I
replaced the 'includes = []' line with:

    
    
    includes = ['Panels.%s' % os.path.basename(os.path.splitext(filename)[0]) for
            filename in glob.glob('Panels//*.py')]
    

In my code where I use the 'import_module' function, it used to use that glob
to import the modules within the Panels directory. Instead, I've hard-coded a
list of modules to include.

This isn't the exact solution I wanted (I don't want to hard-code that list of
panels), but it does seem to work. Unless I discover anything better, this is
what I'll continue to use.

