The Artima Developer Community
Sponsored Link

Python Buzz Forum
python's import is broken

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Victor Ng

Posts: 112
Nickname: victorng
Registered: Aug, 2003

Victor Ng programs Python for money, but he'd be programming Python anyway if he was a bum.
python's import is broken Posted: Apr 26, 2004 3:34 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Victor Ng.
Original Post: python's import is broken
Feed Title: Victor Ng's Weblog
Feed URL: https://blog.crankycoder.com/feed/atom/
Feed Description: Python Feed
Latest Python Buzz Posts
Latest Python Buzz Posts by Victor Ng
Latest Posts From Victor Ng's Weblog

Advertisement

Worst ‘feature’ in Python ever.

Daniel and I have just discovered what is possibly the biggest, nastiest wart in Python today.

Here’s a sample snippet. Consider a module “a.py”

import b 

__all__ =['Foo', ]
class Foo(object): 
    def __init__(self):
        print "foo instantiated"

if __name__ == '__main__':
    Foo()

and another module “b.py”

import a

__all__ =['Bar', ]
class Bar(object): pass

Now try to run “a.py”. You should have no problems.

Now try this “a.py”

from b import Bar

__all__ =['Foo', ]
class Foo(object): 
    def __init__(self):
        print "foo instantiated"

if __name__ == '__main__':
    Foo()

and this “b.py”

from a import Foo

__all__ =['Bar', ]
class Bar(object): pass

Now try to run “a.py”. You’ll get this

Traceback (most recent call last):
  File "a.py", line 1, in ?
    from b import Bar
  File "/private/tmp/b.py", line 1, in ?
    from a import Foo
  File "/private/tmp/a.py", line 1, in ?
    from b import Bar
ImportError: cannot import name Bar

What the hell is happening here? It’s kind of makes sense if you really think about it, but it’s not obvious at all.

Python doesn’t make a distinction between declaring something and executing something. There basically isn’t any declaration going on in Python. Here what I think is happening - the only resource I can find with reference to this problem is the effbot’s Import Confusion page.

The “from foo import baz” style of import is actually checking the modules in memory to see if a particular symbol exists. If it doesn’t then Python tries to import the module.

When you do a circular reference - according to that page I listed - you get an empty entry in the sys.modules. So the second time around when you get to “from b import Bar” - there is already a module ‘b’ in sys.modules and Bar was never defined - so the interpreter blows up.

What is more frustrating is that you can avert the whole problem by just avoiding “from foo import baz” altogether. If you have a deep package/module structure - you can use :

import mypackage.mymodule as mymod

and I’m quite sure you’ll never get this problem.

Now I’ve got to go change literally hundreds of files to solve this circular import problem.

Moral of the story:

Never use “from foo import baz”. Always always always use the “import mypackage” form of import.

The obvious downside of this is that the beginning of your Python scripts will be quite noisy. But heck - what are you going to do about that?

Read: python's import is broken

Topic: Command line parsing Previous Topic   Next Topic Topic: Where to put Python

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use