473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

zip files as nested modules?

Supposing that I have a directory tree like so:

a/
__init__.py
b/
__init__.py
c.py

and b.py has some method (let's call it d) within it. I can, from python, do:

from a.b.c import d
d()

And, that works. Now, suppose I want to have a zipped module under a,
called b.zip. Is there any way that I can accomplish the same thing,
but using the zip file as the inner module?

My directory layout is then

a/
__init__.py
b.zip

And b is a zipfile laid out like

b/
__init__.py
c.py

I tried populating a's __init__ with this:

import zipimport
import os
here = os.path.join(os.getcwd(), __path__[0])
zips = [f for f in os.listdir(here) if f.endswith('.zip')]
zips = [os.path.join(here, z) for z in zips]

for z in zips:
print z
mod = os.path.split(z)[-1][:-4]
print mod
globals()[mod] = zipimport.zipimporter(z).load_module(mod)

All the zip modules appear (I actually have a few zips, but that
shouldn't be important), but their contents do not seem to be
accessible in any way. I could probably put import statements in all
the __init__.py files to import everything in the level below, but I
am under the impression that relative imports are frowned upon, and it
seems pretty bug-prone anyhow.

Any pointers on how to accomplish zip modules being nested within normal ones?
Apr 1 '07 #1
1 1785
Importing modules from zip files was proposed in PEP-273 [1]

Here is how the spec of PEP-273 begins:

'''
Currently, sys.path is a list of directory names as strings. If this
PEP is implemented, an item of sys.path can be a string naming a zip
file archive.
'''

My interpretation of the above is that, to be importable, a zip file
must be explicitly named in sys.path.

So the mere fact that a zip file lies somewhere in a directory which
is part of the sys.path does not make it importable.

Cheers,

Luciano
[1] http://www.python.org/dev/peps/pep-0273/

Apr 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
5
by: TonyB | last post by:
Hi, I've searched the group and need more information and guidance on this issue I need to resolve next week. I work for the local school system and I am working on a way to parse a CSV file of...
2
by: spike grobstein | last post by:
I'm a total Python newbie, so bear with me here... I'm writing a program that has a user-configurable, module-based architecture. it's got a directory where modules are stored (.py files) which...
1
by: Yigit Ozgul | last post by:
Hi, I have a database where i keep 4 tables. the modules table: mdlID mdlName mdlstatus the submodules table:
3
by: Javi | last post by:
I have some doubts about what is the best method to distribute classes in .cpp and .h files: - Should I use a file per class? or should I group similar classes in one file? - Is it good to put a...
10
by: TokiDoki | last post by:
Hello there, I have been programming python for a little while, now. But as I am beginning to do more complex stuff, I am running into small organization problems. It is possible that what I...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
2
by: webcm123 | last post by:
I'm making some changes in files structure in my CMS. I will use more templates for modules. However, the topic of <titleand other ... came back when i was thinking about it. ---- Pre-generating...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.