By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

importing packages from a zip file

P: n/a
Hi all,

I was wondering if this is possible. In python v2.3 the import systems
was extended via PEP302 to cope with packages. *.py files in a directory
hierarchy can be imported as modules each level in the directory
hierarchy needs to contain at least an empty __init__.py file.

eg. With the file system

base/
__init__.py
branch1/
__init__.py
myModule.py

I can import myModule as follows

import base.branch1.myModule

At the same time its possible to store modules in a flat zip-file and
import modules with the following.

from myZip.zip import myModule.py

but is there a way to do both at the same time? eg.

from myZip.zip import base.branch1.myModule

I'm interested in this in the development of mobile code for some Grid
applications :-)

thanks in advance

p
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Peter Tillotson wrote:
... With the file system

base/
__init__.py
branch1/
__init__.py
myModule.py

At the same time its possible to store modules in a flat zip-file and
import modules with the following.

from myZip.zip import myModule.py


Does this work for you? It gives me a syntax error.

Typically, put the zip file on the sys.path list, and import modules
and packages inside it. If you zip up the above structure, you can use:

sys.path.insert(0, 'myZip.zip')
import base.branch1.myModule

--Scott David Daniels
Sc***********@Acm.Org
Jul 19 '05 #2

P: n/a
solution: have to add the zip archives to the PYTHONPATH, can be done in
the env but also as below

import sys, os.path
zipPackages=['base.zip']
for package in zipPackages:
sys.path.insert(0,os.path.join(sys.path[0],package))

import base.branch1.myModule

Peter Tillotson wrote:
Hi all,

I was wondering if this is possible. In python v2.3 the import systems
was extended via PEP302 to cope with packages. *.py files in a directory
hierarchy can be imported as modules each level in the directory
hierarchy needs to contain at least an empty __init__.py file.

eg. With the file system

base/
__init__.py
branch1/
__init__.py
myModule.py

I can import myModule as follows

import base.branch1.myModule

At the same time its possible to store modules in a flat zip-file and
import modules with the following.

from myZip.zip import myModule.py

but is there a way to do both at the same time? eg.

from myZip.zip import base.branch1.myModule

I'm interested in this in the development of mobile code for some Grid
applications :-)

thanks in advance

p

Jul 19 '05 #3

P: n/a
cheers Scott

should have been
from myZip.zip import base.branch1.myModule.py

and no it didn't work, anyone know a reason why this syntax is not
preferred ??

sorry posted the soln again, it works but feels nasty

Scott David Daniels wrote:
Peter Tillotson wrote:
... With the file system

base/
__init__.py
branch1/
__init__.py
myModule.py

At the same time its possible to store modules in a flat zip-file and
import modules with the following.

Does this work for you? It gives me a syntax error.

Typically, put the zip file on the sys.path list, and import modules
and packages inside it. If you zip up the above structure, you can use:

sys.path.insert(0, 'myZip.zip')
import base.branch1.myModule

--Scott David Daniels
Sc***********@Acm.Org

Jul 19 '05 #4

P: n/a
Scott David Daniels <Sc***********@Acm.Org> writes on Wed, 29 Jun 2005 10:36:29 -0700:
Peter Tillotson wrote:
...
from myZip.zip import myModule.py

Does this work for you? It gives me a syntax error.

Typically, put the zip file on the sys.path list, and import modules
and packages inside it. If you zip up the above structure, you can use:

sys.path.insert(0, 'myZip.zip')
import base.branch1.myModule


The alternative is to use a "zipimporter" (from module "zipimport")
and use the "importer protocol" (documented in a PEP).
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.