[BioPython] Problems building biopython-based script with py2exe

Leighton Pritchard lpritc at scri.sari.ac.uk
Fri Jan 30 09:53:18 EST 2004


Over on the Python-Win32 list, Thomas Heller offered some kind advice, and 
thanks to his advice I've now got a much better solution for my problem:

I think my problem is solved with changes only to Bio/__init__.py; no 
modifications were required to FormatIO.py in the end.

At 12:45 30/01/2004, Thomas Heller wrote:
>It might be possible to do this in a more general way.  [...]
>When a module is imported with this mechanism, Bio.config in this case,
>it gets a __loadler__ attribute.  IIRC, this is an instance of the
>builtin zipimporter, and you may be able to get the list of files
>contained in the zip-archive that py2exe builds from it. [...]
>You could even test for the __loader__ attribute first, and trigger the
>special code only when it is present.  [...]

I've taken your advice, and replaced the original registry import code 
(lines 52-55) of Bio/__init__.py with the following:

####

# Import code changed to allow for compilation with py2exe from distutils
     config_imports = __import__("Bio.config", {}, {}, ["Bio"])  # Import 
Bio.config
     if hasattr(config_imports, '__loader__'):                   # Is it in 
zipfile?
         zipfiles = __import__("Bio.config", {}, {}, ["Bio"]).__loader__._files
         x = [zipfiles[file][0] for file in zipfiles.keys() \    # Get only 
Bio.config modules
              if 'Bio\\config' in file]
         x = [name.split('\\')[-1] for name in x]                # Get 
module name
         x = map(lambda x: x[:-4], x)                            # chop off 
'.pyc'
     else:                           # Not in zipfile, get files normally
         x = os.listdir(
             os.path.dirname(config_imports.__file__))
         x = filter(lambda x: not x.startswith("_") and x.endswith(".py"), x)
         x = map(lambda x: x[:-3], x)            # chop off '.py'

###

Which seems to work.

>The general rule with py2exe is: any code accessing the file system or
>using __import__ may create problems in the exe.

I appear to be finding this out the hard way <grin>

> >From a short inspection it seems that Bio.formats is set to an instance
>of Bio.config.FormatRegistry.FormatRegistry.  FormatRegistry again
>contains code that calls __import__.

You're right - that was a great help in tracking down the second part of 
the above solution.


Dr Leighton Pritchard AMRSC
D104, PPI, Scottish Crop Research Institute
Invergowrie, Dundee, DD2 5DA, Scotland, UK
E: lpritc at scri.sari.ac.uk
T: +44 (0)1382 568579           F: +44 (0)1382 568578
PGP key FEFC205C: http://www.keyserver.net http://pgp.mit.edu




More information about the BioPython mailing list