import problems in packages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stéphane Ninin

    #1

    import problems in packages


    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.Handle rFactory import HandlerFactory
    handler = HandlerFactory( ).makeHandler(c ommand)

    main()


    ROOT/Handlers/Handlers.py contains:

    class HandlerBase(obj ect):
    pass


    ROOT/Handlers/HandlerFactory. py contains:

    def HandlerFactory( ):
    import Handlers.Defaul t.HandlerFactor y
    return Handlers.Defaul t.HandlerFactor y.HandlerFactor y()


    ROOT/Handlers/Default/Handlers.py contains:

    from Handlers.Handle rs import HandlerBase

    class Handler(Handler Base):
    pass

    and finally:

    ROOT/Handlers/Default/HandlerFactory. py contains:

    from Handlers.Defaul t.Handlers import Handler

    class HandlerFactory( object):

    def makeHandler(sel f,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(c ommand)
    File "c:\ROOT\Handle rs\HandlerFacto ry.py", line 6, in HandlerFactory
    import Handlers.Defaul t.HandlerFactor y
    ImportError: No module named Default.Handler Factory


    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,




    --
    Stéphane Ninin
    stefnin@alussin an.org

  • Stéphane Ninin

    #2
    Re: import problems in packages

    Also sprach Stéphane Ninin :
    [color=blue]
    >
    >
    > ... 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(c ommand)
    > File "c:\ROOT\Handle rs\HandlerFacto ry.py", line 6, in HandlerFactory
    > import Handlers.Defaul t.HandlerFactor y
    > ImportError: No module named Default.Handler Factory
    >[/color]

    I forgot to mention: I am using Python 2.4.1

    Comment

    • Peter Hansen

      #3
      Re: import problems in packages

      Stéphane Ninin wrote:[color=blue]
      > Traceback (most recent call last):
      > File "main.py", line 8, in ?
      > main()
      > File "main.py", line 5, in main
      > handler = HandlerFactory( ).makeHandler(c ommand)
      > File "c:\ROOT\Handle rs\HandlerFacto ry.py", line 6, in HandlerFactory
      > import Handlers.Defaul t.HandlerFactor y
      > ImportError: No module named Default.Handler Factory[/color]

      Are you sure that's right? You asked it to import
      Handlers.Defaul t.HandlerFactor y but it is apparently importing just
      Default.Handler Factory 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

      Comment

      • Peter Otten

        #4
        Re: import problems in packages

        Stéphane Ninin wrote:
        [color=blue]
        > Also sprach Stéphane Ninin :[/color]

        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!
        [color=blue]
        > Here is the basic structure of the code (I have reduced it first).[/color]

        And nicely so. Not letting similar names for modules, packages and classes
        abound might have been a good idea, too.
        [color=blue]
        > I have an ImportError problem, which is probably correct,
        > but I do not understand it. And how to fix it...[/color]
        [color=blue]
        > 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[/color]
        [color=blue]
        > ROOT/main.py contains:
        > from Handlers.Handle rFactory import HandlerFactory[/color]

        works and triggers
        [color=blue]
        > ROOT/Handlers/HandlerFactory. py contains:
        > import Handlers.Defaul t.HandlerFactor y[/color]

        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.p y

        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



        Comment

        Working...