import issue with classes

Benjamin Kaplan benjamin.kaplan at
Wed Jan 4 01:12:50 EST 2012

On Tue, Jan 3, 2012 at 9:50 PM, Rodrick Brown <rodrick.brown at> wrote:
> I have a class FooB that derives from FooA
> i.e.
> class FooB(FooA):
>   FooA.__init__(self,...)
> Can someone explain why
> Import FooA doesn't work and I need to use from FooA import FooA instead?
> This puzzles me.
> Thanks.
> --
> [ Rodrick R. Brown ]

Because you're coming from Java and thinking that Python behaves the
same way, which isn't true. In Java, every code file consists of a
single public class with the same name as the file. In Python, a
module is an object consisting of many other objects, including
classes, functions, and variables. There's no need for the class name
to be anything related to the file name. By convention, modules
(files) have lowercase names. So lets call your file "" with a
class FooA inside it, just so we can distinguish between them.

"import fooA" searches for a file called in a set of
directories, finds it, compiles it, and binds the now-created module
object to the name fooA. The module object fooA has a class "FooA"
inside it. They're two separate things.

"from fooA import FooA" searches for, just like before. But
instead of giving you the whole module, it just looks for FooA and
binds it to the same name in the current context.

More information about the Python-list mailing list