[code.view]

[top] / python / PyMOTW / pkgutil / pkgutil_get_data_zip.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Loading data from a zipfile.
     """
     #end_pymotw_header
     
     import pkgutil
     import zipfile
     import sys
     
     # Create a ZIP file with code from the current directory
     # and the template using a name that does not appear on the
     # local filesystem.
     with zipfile.PyZipFile('pkgwithdatainzip.zip', mode='w') as zf:
         zf.writepy('.')
         zf.write('pkgwithdata/templates/base.html',
                  'pkgwithdata/templates/fromzip.html',
                  )
     
     # Add the ZIP file to the import path.
     sys.path.insert(0, 'pkgwithdatainzip.zip')
     
     # Import pkgwithdata to show that it comes from the ZIP archive.
     import pkgwithdata
     print 'Loading pkgwithdata from', pkgwithdata.__file__
     
     # Print the template body
     print '\nTemplate:'
     print pkgutil.get_data('pkgwithdata', 'templates/fromzip.html').encode('utf-8')
     

[top] / python / PyMOTW / pkgutil / pkgutil_get_data_zip.py

contact | logmethods.com