[Chicago] import question

Pete pfein at pobox.com
Tue Jan 22 23:33:25 CET 2008


On Tuesday January 22 2008 4:59:10 pm Massimo Di Pierro wrote:
> Hello everybody,
>
> I was hoping you could help me.
>
> Say I have a module called test
>
> test/
>     __init__.py
>     other.py
>
> and __init__.py says
>
>     from test.other import something
>
> so that I can
>
>     from test import something
>
> Now I do not want to install the module but I want to place it under
> a folder called contrib:
>
> contrib/
>     test/
>        __init__.py
>        other.py
>
> and I want to be able to say
>
>    from contrib.test import something
>
> Now __init__ fails to find test.other because that's contrib.test.other.

I'm presuming contrib is a package here, ie, that contrib/__init__.py exists.
That said, under Python 2.5+, in contrib/test/__init__.py you can write:

from .other import something

That's a relative import, see http://docs.python.org/whatsnew/pep-328.html

If you're targeting <2.5, you'll need to do one of the alternatives you 
suggested - (1) is clearly better, as you noted.

-- 
Peter Fein   ||   773-575-0694   ||   pfein at pobox.com
http://www.pobox.com/~pfein/   ||   PGP: 0xCCF6AE6B
irc: pfein at freenode.net   ||   jabber: peter.fein at gmail.com


More information about the Chicago mailing list