import statement - package visibility problem

Laszlo Zsolt Nagy gandalf at geochemsource.com
Thu Apr 7 15:11:53 EDT 2005


Paul Clinch wrote:

>>    
>>
>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.
>  
>
You are totally right. I was trying to create an example but Python 
found something on my PYTHONPATH.
Sorry for the confusion, I sent a bad example. Well, could not be so bad 
because you understood the point.
I'll try to create a new good example today and make it available.

>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.
>  
>
I agree. Sometimes I need to import Lib.Server.Db but sometimes I need 
to import Lib.Server.Db.Adapters.PostgreSQLConnection.
Importing from a not package related source code is not a problem, 
really. My problem is about importing inside a package.

Lib/Server/Db/__init__.py __can__  import Lib/Server/Db/Adapters usign 
relative paths, but

Lib/Server/Db/Adapters/PostgreSQLConnection.py  __cannot__  import from 
Lib/Server/Db using relative paths

Of course I can do the latter import using absolute paths but that is 
something I would like to avoid. In other words:
it is possible to import using relative lib paths only in one direction. 
Why is that? Why can't I import something
"from the containing package"?

Why I would like to do that? Please see my reasons in my original 
e-mail. If you think that I want a programatically
bad thing, please tell me what is the right way to do it. I'm sorry if I 
look demanding.  I'm open to any suggestions.
I can hardly find a design problem in the language itself, I believe 
these problems are just mine. I have a bug in my
mind. :-) Please help me to catch it.

>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. 
>
Ok, in the example, you will see that this is not possible. I'm going to 
upload it soon.

>Or you can define any rules for mapping files and directories to the name space
>you desire.
>  
>
How to do that? By hacking?


-- 
_________________________________________________________________
  Laszlo Nagy		      web: http://designasign.biz
  IT Consultant		      mail: gandalf at geochemsource.com

     		Python forever!





More information about the Python-list mailing list