Package organization

Graham Ashton gashton at
Tue Dec 16 09:52:05 CET 2003

On Wed, 19 Nov 2003 14:23:33 -0800, Robert Ferrell wrote:

> Graham Ashton <gashton at> wrote in message news:<pan.2003. at>...

>> Also, because you're importing something from inside a module using the
>> "from" style imports you're risking running into circular reference
>> problems. In my experience from imports are best avoided, unless you're
>> importing a module from a package.
> I don't understand this comment.  How does "from" style importing
> increase the risk of circular imports?

Imagine you've got two modules, spam and eggs. 

When you import a module it's top level statements get evaluated. Imagine
that spam imports something from eggs. The interpreter (whilst importing
spam) pauses to import eggs. What if eggs tries to import something from

You might think, ah, that's a circular import. But it isn't necessarily.
You see, when it starts importing spam the interpreter stores an empty
module object in sys.modules and fills in the details once it's
finished importing spam. Consequently when it's trying to import spam
again from inside eggs it finds a module reference in sys.modules for spam
and can carry on happily.

Things only break on you if eggs is trying to refer to something inside
spam that the original import of spam hasn't filled in yet.

I'll try and give you an example. Here's the spam module:

    import eggs

    def count_spam():
        return 1

And the eggs module:

    from spam import count_spam

    def count_eggs_and_spam():
        return count_spam() + 1

If your main script then tries to import spam it'll blow up:

% python
Python 2.3.2 (#1, Dec  8 2003, 10:49:17)
[GCC 3.3.2 20031022 (Gentoo Linux 3.3.2-r3, propolice)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import spam
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "", line 1, in ?
    import eggs
  File "", line 1, in ?
    from spam import count_spam
ImportError: cannot import name count_spam

It's because the spam module in sys.modules hasn't been filled in yet, as
the interpreter put off importing the contents of spam until the "import
eggs" statement returned. Replace the from import with a normal import and
the problem goes away. Here's a replacement eggs module:

    % cat
    import spam
    def count_eggs_and_spam():
        return spam.count_spam() + 1

It's no longer an issue as the spam.count_spam() function doesn't get
evaluated until the count_eggs_and_spam() function is called, which is
long after both the modules have been fully imported.

Stay away from from, it's dangerous. And in my opinion you get less clear

Incidentally, I learnt the nitty gritty of this from Martelli's
Python in a Nutshell. Recommended.

-- Graham

More information about the Python-list mailing list