import statement - package visibility problem

Paul Clinch pclinch at internet-glue.co.uk
Thu Apr 7 14:24:04 EDT 2005


Laszlo,
For :-
> Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on 
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import Lib
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "Lib\__init__.py", line 1, in ?
>     import Server
>   File "Lib\Server\__init__.py", line 1, in ?
>     import Db
>   File "C:\Python\Lib\Db\__init__.py", line 29, in ?
>     import      Adapters
>   File "C:\Python\Lib\Db\Adapters\__init__.py", line 21, in ?
>     import FireBirdConnection
>   File "Db\Adapters\FireBirdConnection.py", line 27, in ?
> ImportError: No module named DatabaseConnection
>  >>>
> 
I get:
>>> import Lib
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named Lib

I guess there's a Lib/__init__.py.

But onto the point you're making. I think its possibly a mis-viewing
of the package idea in Python. A package creates a name space. If you
create Lib/Server/Db with all the __init__.py files, its because you
want to import Lib.Server.Db,  rather than a way of organising your
source files.

If you want to have a single name space, and keep the nested
arrangement of directories, you can add python code in Lib/__init__.py
to traverse directories and import packages that you find. Or you can
define any rules for mapping files and directories to the name space
you desire.

Regards, Paul



More information about the Python-list mailing list