import problems in packages

S

Stéphane Ninin

Hello all,


I have an ImportError problem, which is probably correct,
but I do not understand it. And how to fix it...


Here is the basic structure of the code (I have reduced it first).

ROOT:
/main.py
/Handlers/__init__.py (empty)
/Handlers/Handlers.py
/Handlers/HandlerFactory.py
/Handlers/Default/__init__.py (empty)
/Handlers/Default/Handlers.py
/Handlers/Default/HandlerFactory.py

Now, for the content of the files:

ROOT/main.py contains:
def main():
command = 'A'
from Handlers.HandlerFactory import HandlerFactory
handler = HandlerFactory().makeHandler(command)

main()


ROOT/Handlers/Handlers.py contains:

class HandlerBase(object):
pass


ROOT/Handlers/HandlerFactory.py contains:

def HandlerFactory():
import Handlers.Default.HandlerFactory
return Handlers.Default.HandlerFactory.HandlerFactory()


ROOT/Handlers/Default/Handlers.py contains:

from Handlers.Handlers import HandlerBase

class Handler(HandlerBase):
pass

and finally:

ROOT/Handlers/Default/HandlerFactory.py contains:

from Handlers.Default.Handlers import Handler

class HandlerFactory(object):

def makeHandler(self,command):
print 'HERE'
return Handler()




.... when I start main.py, I get:

Traceback (most recent call last):
File "main.py", line 8, in ?
main()
File "main.py", line 5, in main
handler = HandlerFactory().makeHandler(command)
File "c:\ROOT\Handlers\HandlerFactory.py", line 6, in HandlerFactory
import Handlers.Default.HandlerFactory
ImportError: No module named Default.HandlerFactory


Using a bit different structure (with relative imports), I also had an
ImportError in ROOT/Handlers/Default/Handlers.py


I guess there is something I do not understand in the way Python looks for
the correct file in a package...

Can someone give me some explanations about what is happening here ?
And a way to solve that ?

Thanks for any help,
 
S

Stéphane Ninin

Also sprach Stéphane Ninin :
... when I start main.py, I get:

Traceback (most recent call last):
File "main.py", line 8, in ?
main()
File "main.py", line 5, in main
handler = HandlerFactory().makeHandler(command)
File "c:\ROOT\Handlers\HandlerFactory.py", line 6, in HandlerFactory
import Handlers.Default.HandlerFactory
ImportError: No module named Default.HandlerFactory

I forgot to mention: I am using Python 2.4.1
 
P

Peter Hansen

Stéphane Ninin said:
Traceback (most recent call last):
File "main.py", line 8, in ?
main()
File "main.py", line 5, in main
handler = HandlerFactory().makeHandler(command)
File "c:\ROOT\Handlers\HandlerFactory.py", line 6, in HandlerFactory
import Handlers.Default.HandlerFactory
ImportError: No module named Default.HandlerFactory

Are you sure that's right? You asked it to import
Handlers.Default.HandlerFactory but it is apparently importing just
Default.HandlerFactory instead? This seems unexpected to me, though
perhaps it can happen in certain situations. It's a hint, anyway.

Are the earlier four lines which you did show us relevant? (i.e. if
that's line six, what was on lines one, two, three, and four?)

-Peter
 
P

Peter Otten

Stéphane Ninin said:
Also sprach Stéphane Ninin :

Sollte es denn möglich sein! Dieser alte Heilige hat in seinem Walde noch
Nichts davon gehört... that intra-package import takes precedence over
absolute import!
Here is the basic structure of the code (I have reduced it first).

And nicely so. Not letting similar names for modules, packages and classes
abound might have been a good idea, too.
I have an ImportError problem, which is probably correct,
but I do not understand it. And how to fix it...

ROOT/main.py contains:
from Handlers.HandlerFactory import HandlerFactory

works and triggers
ROOT/Handlers/HandlerFactory.py contains:
import Handlers.Default.HandlerFactory

which tries to import

ROOT/Handlers/Handlers/Default/HandlerFactory.py, but unfortunately
ROOT/Handlers/Handlers[.py] is not a package and therefore doesn't contain
a Default package. Currently, when you have

amodule.py
apackage/amodule.py
apackage/anothermodule.py

and try to import amodule into anothermodule, there is no way to tell Python
that you mean amodule.py and not apackage/anmodule.py.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,006
Latest member
TerranceCo

Latest Threads

Top