How to import only one module in a package when the package has already imports the modules?

Duncan Booth duncan.booth at invalid.invalid
Sat Oct 31 18:47:16 CET 2009

Peng Yu <pengyu.ut at> wrote:

> I'm wondering if there is a way to make the following two things hold.
> Thank you1
> 1. When I 'import test', I can refer to class A as 'test.A'.
> 2. When I 'import test.A', I can refer to class A as 'test.A.A' and
> class B shall not be imported.
No. Either import adds the name 'test' to the current namespace. That name 
in each case references the same thing.

Your simplest solution would be to give the sub-modules lowercase 
filenames, then you can do:

   import test


   import test.a

or even

   import test.a

It would probably be best though just to be consistent as to how you 
reference the classes: define a public interface for your package and stick 
to it.

More information about the Python-list mailing list