Import no longer works in Python 2.3.x

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Whiteley

    Import no longer works in Python 2.3.x

    I have some python code in a directory called customModules and this sits
    under the main directory which contains a piece of server.py code. Under
    2.1.x and 2.2.x, I did the following in server.py :

    import time, sys
    sys.path += ['./customModules']

    from requestHandler import inputHandler

    and that allowed me to reference the requestHandler (stored in the
    customModules directory) code without having to use
    customModules.r equestHandler every time. Under 2.3.x this doesn't work
    anymore and I get a "ImportErro r: cannot import name inputHandler" error.
    Can anyone shed some light on why this is and how I can get around it
    please.

    Code that demonstrates this exact problem is at
    http://mss.cynetix.co.uk/download.htm - running server.py will work fine
    under 2.2 but not under 2.3.

    Many thanks
    Matt Whiteley


  • Graham Fawcett

    #2
    Re: Import no longer works in Python 2.3.x

    "Matt Whiteley" <matt@cynetix.c o.uk> wrote in message news:<w6wWb.1$M Q1.0@news-binary.blueyond er.co.uk>...[color=blue]
    > I have some python code in a directory called customModules and this sits
    > under the main directory which contains a piece of server.py code. Under
    > 2.1.x and 2.2.x, I did the following in server.py :
    >
    > import time, sys
    > sys.path += ['./customModules']
    >
    > from requestHandler import inputHandler
    >
    > and that allowed me to reference the requestHandler (stored in the
    > customModules directory) code without having to use
    > customModules.r equestHandler every time. Under 2.3.x this doesn't work
    > anymore and I get a "ImportErro r: cannot import name inputHandler" error.
    > Can anyone shed some light on why this is and how I can get around it
    > please.[/color]

    Unless there's something going on here that's not clear from your
    example, you can just add an __init__.py to your customModules
    directory, and then use

    from customModules.r equestHandler import inputHandler

    Adding __init__.py creates a "pacakge" out of the subdirectory, and
    obviates the sys.path manipulations. See the "Packages" section at
    http://www.python.org/doc/current/tut/node8.html for more information.

    -- Graham

    Comment

    • Paul Clinch

      #3
      Re: Import no longer works in Python 2.3.x

      Interesting, I note that there has been some code revision around the
      import. See the whats new in python 2.3, Importing Modules from Zip
      Archives and New Import Hooks.

      If a full pathname is used, eg.
      /home/paul/projects/test/customModules
      the code works as before. This would definitely be safer since the
      current directory may change in the future, causing the import to fail
      anyway.

      Regards, Paul Clinch

      Comment

      • Matt Whiteley

        #4
        Re: Import no longer works in Python 2.3.x

        Hmmm, that messes up all my imports from all over my code and it's all
        worked before so I'd rather not. I also wanted the code to be able to run
        straight out of the box without people having to hard code the paths.

        Thanks anyway though!

        "Graham Fawcett" <graham__fawcet t@hotmail.com> wrote in message
        news:e9570f37.0 402112041.1890c 686@posting.goo gle.com...[color=blue]
        > "Matt Whiteley" <matt@cynetix.c o.uk> wrote in message[/color]
        news:<w6wWb.1$M Q1.0@news-binary.blueyond er.co.uk>...[color=blue][color=green]
        > > I have some python code in a directory called customModules and this[/color][/color]
        sits[color=blue][color=green]
        > > under the main directory which contains a piece of server.py code. Under
        > > 2.1.x and 2.2.x, I did the following in server.py :
        > >
        > > import time, sys
        > > sys.path += ['./customModules']
        > >
        > > from requestHandler import inputHandler
        > >
        > > and that allowed me to reference the requestHandler (stored in the
        > > customModules directory) code without having to use
        > > customModules.r equestHandler every time. Under 2.3.x this doesn't work
        > > anymore and I get a "ImportErro r: cannot import name inputHandler"[/color][/color]
        error.[color=blue][color=green]
        > > Can anyone shed some light on why this is and how I can get around it
        > > please.[/color]
        >
        > Unless there's something going on here that's not clear from your
        > example, you can just add an __init__.py to your customModules
        > directory, and then use
        >
        > from customModules.r equestHandler import inputHandler
        >
        > Adding __init__.py creates a "pacakge" out of the subdirectory, and
        > obviates the sys.path manipulations. See the "Packages" section at
        > http://www.python.org/doc/current/tut/node8.html for more information.
        >
        > -- Graham[/color]


        Comment

        • Matt Whiteley

          #5
          Re: Import no longer works in Python 2.3.x

          Hmm, didn't want to have to hard code paths really. I can't understand
          what's been broke all of a sudden. I know there's a fair bit of thought gone
          into the os.path logic but I can't seem to find anything that relates
          directly to my problem.

          Thanks Anyway,
          Matt

          "Paul Clinch" <pclinch@intern et-glue.co.uk> wrote in message
          news:8cf2994e.0 402120714.1b0e6 ff0@posting.goo gle.com...[color=blue]
          > Interesting, I note that there has been some code revision around the
          > import. See the whats new in python 2.3, Importing Modules from Zip
          > Archives and New Import Hooks.
          >
          > If a full pathname is used, eg.
          > /home/paul/projects/test/customModules
          > the code works as before. This would definitely be safer since the
          > current directory may change in the future, causing the import to fail
          > anyway.
          >
          > Regards, Paul Clinch[/color]


          Comment

          Working...