Hi All!
I have the following structure:
/Lib/Server/Db/ __init_.py DatabaseConnection.py Adapters/ __init__.py FireBirdConnection.py
Important file contents are:
/Lib/Server/Db/__init__.py:
import DatabaseConnection import Adapters
/Lib/Server/Db/DatabaseConnection.py:
class DatabaseConnection(object): pass
/Lib/Server/Db/Adapters/__init__.py
import FireBirdConnection
/Lib/Server/Db/Adapters/FireBirdConnection.py:
from DatabaseConnection import DatabaseConnection class FireBirdConnection(DatabaseConnection): pass
Here is the problem. I go to the directory where 'Lib' resides. Then I do:
C:\Temp\ccc>python
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
>>>
My problem is that Lib/Server/Db/Adapters/FireBirdConnection.py does not see Lib/Server/Db/DatabaseConnection.py.
Of course I read the documentation about the import statement but I do not see a good solution.
Possible solutions and their drawbacks:
Solution 1:
Put the 'Db' directory into my PYTHONPATH or append it to "sys.path". It is too bad. This could make my 'Db' module
visible but this is not a good solution. For example, I can have this module structure:
Lib/ Client/ Db/ Adapters/ Server/ Db/ Adapters/
E.g. there could be two "Db" modules, one for Client and one for Server. Clearly, I cannot add EVERY module path to sys.path. It would be ambiguous to import 'Adapters', for example.
Solution 2:
Add the outermost 'Lib' directory to sys.path and use fully qualified module names. In the example above I could use this:
/Lib/Server/Db/Adapters/FireBirdConnection.py:
from Lib.Server.Db.Adapters.DatabaseConnection import DatabaseConnection
In this case FireBirdConnection.py would be dependent on the full module path. Otherwise it is independent on the whole module path, it only depends on the 'upper level' module, regardless of its name. So here are the problems with this solution:
- What if I want to rename 'Server' to 'Middletire'? Should I change all of my files inside the 'Midletire' dir?
-What if I would like to unify the server and client Db sides? Should I rename "from Lib.Server.Db.Adapters.DatabaseConnection import DatabaseConnection" to "from Lib.Db.Adapters.DatabaseConnection import DatabaseConnection" in all files?
- Lastly, the import statements are too long. They are hard to manage.
I would like to use something like
from __uppermodule__.DatabaseConnection import DatabaseConnection
but probably there is a standard Pythoninc way to do it. Just I can't find it.
Maybe we can start a new PEP on this. :-)
Please advise.
p.s.: sorry for the long e-mail
-- _________________________________________________________________ Laszlo Nagy web: http://designasign.biz IT Consultant mail: [EMAIL PROTECTED]
Python forever!
-- http://mail.python.org/mailman/listinfo/python-list