views:

134

answers:

3

In my working python directory I create:

packagename/__init__.py
packagename/modulename.py
test.py

In modulename.py I create some empty class:

class Someclass(object):
   pass

in test.py:

import packagename
packagename.modulename.Someclass()

Why I can't call packagename.modulename.someclass() in test.py ?

AttributeError: 'module' object has no attribute 'modulename'

I understand that the right way is:

import packagename.modulename

or

from packagename import modulename

But I do not understand why I get this error in my case.

Update:

In other words is there any way to import a package's content with all modules in the distinct namespace? I need correct pythonic expression for:

from packagename import * as mynamespace
A: 

Some packages are very large and importing everything within them would take a lot of time. It's better to offer fine granularity for imports, so the user only imports what he really needs.

bayer
+1  A: 

If you had 100 modules in the "packagename" package, would you want them all to be imported automatically when you imported just the top-level name? That wouldn't usually be a good idea, so Python doesn't do it.

If you want to have that particular module imported automatically, just include it in the __init__.py like so:

from packagename import modulename

Alternatively, using Python 2.5 or 2.6:

from __future__ import absolute_import
from . import modulename

(In later versions, you can ditch the from __future__ part.)

Edit: there is no built-in mechanism to ask Python to import all possible submodules inside a package. That's not a common use case, and it's better handled explicitly by having your __init__.py import precisely those things that you want. It would be possible to put something together to do the job (using __import__() and such) but it is better in most cases just to explicitly import all submodules as described.

Peter Hansen
I need list all packages's modules in packagename/__init_.py, right?I think I need correct python expression for "from packagename import * as mynamespace".
Alex
I think that's spelled simply `import packagename as mynamespace`.
Peter Hansen
A: 

Python does not automatically recurse and import subpackages. When you say:

import packagename

That is all it imports. If you say:

import packagename.modulename

Then it first imports packagename, then imports packagename.modulename and assignes a reference to it as an attribute of packagename. Therefore, when you say in code:

packagename.modulename.Someclass()

Python is just using 100% normal attribute lookups. First lookup the packagename variable in the current namespace. Then lookup the modulename attribute of the packagename object. Then lookup the Someclass attribute of the modulename object.

If you neglected to import packagename.modulename, then there is plainly no attribute on packagename called modulename, and henceforth the AttributeError.

I suggest you pop onto the command line and import something, then use dir() to examine it. Then import a subpackage, and use dir() again. You will quickly see the difference.


Lastly, the syntax:

from packagename.modulename import SomePackage

Is essentially the same as this:

import packagename.modulename
SomePackage = packagename.modulename.SomePackage

(of course, it's implemented differently, but more or less the same result).

Does that help?

gahooa
@gahooa: Thank you for your answer, but is there any way to import all modules from package in separate namespace? Something like "import packagename.*"
Alex