Import no longer works in Python 2.3.x

Graham Fawcett graham__fawcett at hotmail.com
Wed Feb 11 23:41:49 EST 2004


"Matt Whiteley" <matt at cynetix.co.uk> wrote in message news:<w6wWb.1$MQ1.0 at news-binary.blueyonder.co.uk>...
> 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.requestHandler every time. Under 2.3.x this doesn't work
> anymore and I get a "ImportError: cannot import name inputHandler" error.
> Can anyone shed some light on why this is and how I can get around it
> please.

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.requestHandler 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



More information about the Python-list mailing list