[Tutor] Import package module problem

mandel at themacaque.com mandel at themacaque.com
Tue May 12 09:39:00 CEST 2009


> Le Mon, 11 May 2009 21:44:09 +0100 (BST),
> mandel at themacaque.com s'exprima ainsi:
>
>> Hello there,
>>
>> I have just started working with python and I have some issues
>> understanding how I should be importing modules from packages. Curretly
>> I
>> have the following tree structure:
>>
>> general/
>>     __init__.py
>>     address_book.py
>> groups/
>>     __init__.py
>>     contact_group.py
>>
>> where groups and general are in the same level and all the __init__.py
>> files are empty.
>>
>> In address_book.py I want to import the ContactGroup which is at
>> contact_group.py. I have try the following posibilities:
>>
>> from ..groups.contact_group import ContactGroup
>> from groups.contact_group import ContactGroups
>> from groups.contact_group import *
>>
>> But I cannot get it working. I know that the third option will not work
>> because I have not set up __all__ but I do not understand why it does
>> not
>> work. I have read books and tutorials about it, but I cannot get it.
>>
>> Am I doing something wrong? I would really appreciate a hand or pointer
>> to
>> s step by step example.
>
> Not sure at all, 'cause python packaging is still rather unclear for me,
> but I guess you should have module (or other names thingies) imported
> inside __init__.py; and that's precisely their main purpose (in addition
> to possibly running startup code). If I'm right, simply writing in
> groups/__init__.py
>    import contact_group
> or
>    from contact_group import ContactGroups
> should do the job. Actually, python does *not* automatically populate
> (sub-)packages' dict of names with the files in the directory (as I would
> also have expected).
>

Lets see if I understand what you mean, in my example I should add an
import inside __init__.py to be able to use relative imports:

/general
   __init__.py >> from address_book import AddressBook
   address_book.py
/groups
   __init__.py >> from contact_group import ContactGroup
   contact_group.py

Lets say I have the above, would I be able to use the following?

from ..groups.contact_group import ContactGroup

I'm trying to use this type of import to be able to bundle my unit tests
with my code and let devels work with it. I have read somewhere that
__all__ can do the trick   but it did not say how.

> [The reason why I still do not understand what python packaging, esp. the
> use of __init__ files, is supposed to bring to the developper and/or the
> user. I've read several docs several times, but they all say the same,
> even using the same examples, and none points to the actual purpose.
> Having a 'normal' py file importing names intended to be exported would do
> the same job. Anyone?]
>
> Denis
>
>> Kr,
>>
>> Manuel
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> ------
> la vita e estrany
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>





More information about the Tutor mailing list