[Tutor] Re: using multiple files

Abel Daniel abli at freemail.hu
Fri Nov 21 10:29:23 EST 2003


Daniel Ehrenberg  writes:

> I am working on an application to act as a client for
> Wikipedia (see wikipedia.org for the website and
> mediawiki.org for the current php/mysql software). It
> will be based on the pywikipediabot library, which is
> split up into an annoyingly large number of files.
http://cvs.sourceforge.net/viewcvs.py/pywikipediabot/pywikipedia/
contains around 20 files. That doesn't sound too bad to me.

> Is it possible to import those modules from files that
> will be distributed with the main script,
You mean as opposed to import them from a system-wide install?

Like you install pywikipediabot first, and then from your program
import modules the same way as, say importing the 'os' module (where
you don't care where the os module is or how many files it is, etc.)

I guess adding the directory that contains the files to the front of
sys.path will guarantee that you will import from the files you
want. (Even if there is a module with the same name installed
elsewhere.)

But this wouldn't look too nice to me. What would you gain by this?
Librarys are librarys and I don't think you should 'statically link'
you program with one. If it is installed system-wide, why would you
want to use your (not-sytem-widely-installed) version?
(Maybe I don't fully understand what you want.)

If you want to avoid things like
import module.foo, module.bar, module.foobar.deep.hierarchy.stuff
(i.e. ease importing for the users)
then I think you should make an __init__.py

For example by putting

    from foobar.deep.hierarchy import stuff

in it, you can access stuff as module.stuff after 'import module',
so your users shouldn't have to care how many files you have.
(Or at least that's the idea, I guess. I'm not an expert in this as I
didn't have to do it so far.)

> or preferably import from a bz2 archive?
As of version 2.3, you can import from zipfiles:
from http://python.org/2.3/NEWS.txt:
- Import from zipfiles is now supported.  The name of a zipfile placed
  on sys.path causes the import statement to look for importable Python
  modules (with .py, pyc and .pyo extensions) and packages inside the
  zipfile.  The zipfile import follows the specification (though not
  the sample implementation) of PEP 273.  The semantics of __path__ are
  compatible with those that have been implemented in Jython since
  Jython 2.1.

(I'm not sure whether it works with bz2 archives)

-- 
Abel Daniel



More information about the Tutor mailing list