[Chicago] import question

Cosmin Stejerean cstejerean at gmail.com
Tue Jan 22 23:36:06 CET 2008


adding both contrib and test in your example to the path is indeed bad since
as far as Python is concerned contrib.test and test refer to two separate
modules.

The __init__.py inside of the test package contains "from test.other import
something"? It can simply be

from other import something

which would bring something into test. This won't work in more complicated
situations like

test/
 __init__.py
 package1/
   __init__.py
   mod1.py
 package2/
  __init__.py
   mod2.py

If mod2.py from package2 needs to reference something from package1 it had
no choice traditionally but to refernece package1 as test.package1 (which
causes exactly the problems you describe). This is the reason relative
imports were added but it does require patching the library in question. I
don't think there is any way around this other than:

1. put the test under an external directory that is added to the path, not
inside another package
2. patch the library by either
   a) using relative imports (breaks compatibility with 2.4 and possibly 2.5
)
   b) refactor using a library like py-ioc (http://code.google.com/p/py-ioc/),
a library that I wrote to solve similar problems

- Cosmin

On Jan 22, 2008 3:59 PM, Massimo Di Pierro <mdipierro at cs.depaul.edu> 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.
>
> Two natural solutions are:
> 1) edit __init__.py and replace test.other with write
> contrib.test.other. This is not acceptable for me since I do not want
> to maintain the test module.
> 2) add contrib to the path. This is also not acceptable because
> creates countless problems with py2exe and py2app. I have many
> modules and files with similar issues.
>
> Does anybody know of a third solution?
>
> Massimo
>
> P.S. No need to say this has something to do with www.web2py.com
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>



-- 
Cosmin Stejerean
http://blog.offbytwo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20080122/323fbe49/attachment.htm 


More information about the Chicago mailing list