Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

from import and __init__.py

1 view
Skip to first unread message

egbert

unread,
Mar 25, 2010, 6:16:32 AM3/25/10
to pytho...@python.org
When I do 'from some_package import some_module'
the __init__.py of some_package will be run.
However, there will not be anything like a package-module,
and the effects of __init__.py seem all to be lost. Is that true ?
Or can I still do something useful with __init__.py ?
e
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
========================================================================

Steve Holden

unread,
Mar 25, 2010, 7:42:20 AM3/25/10
to pytho...@python.org
egbert wrote:
> When I do 'from some_package import some_module'
> the __init__.py of some_package will be run.
> However, there will not be anything like a package-module,
> and the effects of __init__.py seem all to be lost. Is that true ?
> Or can I still do something useful with __init__.py ?
> e

If I understand correctly what you mean byt a "package-module" then
__init__.py is exactly what you are looking for.

Many packages are built with an empty __init__.py because they are
intended mostly to build a tree-like set of namespaces, but __init__.py
*is* run when the package is imported, and its namespace is bound to the
name of the package within the importing program. So if you have code
you want to run when the package is imported, put it there.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

Terry Reedy

unread,
Mar 25, 2010, 12:43:13 PM3/25/10
to pytho...@python.org
On 3/25/2010 6:16 AM, egbert wrote:
> When I do 'from some_package import some_module'
> the __init__.py of some_package will be run.
> However, there will not be anything like a package-module,
> and the effects of __init__.py seem all to be lost. Is that true ?

No. If you do

from sys import modules
print(modules.keys())

you will see both some_package and some_package.some_module among the
entries. The first is the result of executing some_package/__init__.py.
As usual, that code will *not* be re-exectured on subsequent imports
involving some_package.


> Or can I still do something useful with __init__.py ?
> e

Some packages put something like 'from _default_stuff import *' in
__init__.py with the intention that the package by used as

import package

perhaps [optionally] followed by

import package.specialized_stuff

Terry Jan Reedy

egbert

unread,
Mar 25, 2010, 6:28:48 PM3/25/10
to Terry Reedy, pytho...@python.org
On Thu, Mar 25, 2010 at 12:43:13PM -0400, Terry Reedy wrote:
> On 3/25/2010 6:16 AM, egbert wrote:
> >When I do 'from some_package import some_module'
> >the __init__.py of some_package will be run.
> >However, there will not be anything like a package-module,
> >and the effects of __init__.py seem all to be lost. Is that true ?
>
> No. If you do
>
> from sys import modules
> print(modules.keys())
>
> you will see both some_package and some_package.some_module among
> the entries.
Yes, you are right. And I can reach everything with
modules['some_package']
or variants thereof.
Thanks,
egbert

Gregory Ewing

unread,
Mar 27, 2010, 1:18:40 AM3/27/10
to
egbert wrote:

> Yes, you are right. And I can reach everything with
> modules['some_package']
> or variants thereof.

Although note that the usual way to get it would be
to simply do

import some_package

--
Greg

0 new messages