module and namespace
Peter Otten
__peter__ at web.de
Fri Apr 18 07:48:40 EDT 2014
Egon Frerich wrote:
[Egon, please post in plain test, not html. Thank you]
> I have a problem with a namespace. There is a module mptt (actally from
> Django). If I import this module with the interpreter it tells me the
> namespace:
>
> Python 3.3.5 (default, Apr 12 2014, 23:34:20)
> [GCC 4.6.3] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>
> import mptt
> print(mptt)
>
> <module 'mptt' from './mptt/__init__.py'>
>
>
> If I import mptt in my program then there is no ImportError but the
> namespace is broken:
>
> <module 'mptt' (namespace)>
>
> (This is the output with print after the import).
>
> What is the meaning? When does this happened?
Basically Python 3 allows for packages to omit the __init__.py
$ mkdir aaa
$ python3 -c'import aaa; print(aaa)'
<module 'aaa' (namespace)>
$ touch aaa/__init__.py
$ python3 -c'import aaa; print(aaa)'
<module 'aaa' from './aaa/__init__.py'>
Namespace packages have advantages when you want to merge submodules from
multiple places into one package. See
<http://legacy.python.org/dev/peps/pep-0420/> for the details.
Your actual problem is probably that the parent directory for the mptt
package is not in your sys.path, but an unrelated directory with a mptt
subdirectory (that may not contain any python code) is. This is the
disadvantage of namespace packages -- any old directory may be mistaken for
a package.
As to fixing the problem -- I don't know much about django, but you may need
to invoke the interactive interpreter with
$ python3 manage.py shell
More information about the Python-list
mailing list