[Zope3-dev] Importing

Guido van Rossum guido@python.org
Tue, 11 Dec 2001 13:49:19 -0500


> If you have a file structure like:
> 
> /something/__init__.py
> /something/somemodule.py
> /something/abit/__init__.py
> /something/abit/somemodule2.py
> 
> ...you'd import the package like:
> 
> import something
> 
> BUT, how would you import a method/class in somemodule.py _from_ somemodule2.py?
> 
> I found the zope notion of a global name 'Products' very useful for this, and I
> could only do the same in a non-Zope environment doing the evillest of hacks,
> along the lines of:
> 
> something_package = string.join(string.split(__name__,'.')[:-2],'.')
> exec "import %s.abit" % something_package
> 
> Now, how should I be doing this? (and in Zope 3?)

Ah, the relative import fallacy.  Why is it required to be able to
reparent a package without changing the source?  To me, always using
absolute paths makes the most sense:

    from something.somemodule import someclass

works independently from where the caller lives.

--Guido van Rossum (home page: http://www.python.org/~guido/)