[Tutor] import question

Alan Gauld alan.gauld at freenet.co.uk
Sat Jan 8 01:09:18 CET 2005


> I'm trying to import classes and functions across
> several directories.
> Here's a simple example of my filesystem:
> /Defs
>       base.py
>       /One
>             a.py
>             b.py
>       /Two
>             c.py
>
> In a.py I have:
> ###
> import b
> ###
> This results in an error:
> ImportError: No module named b
>
> My sys.path contains the absolute path to /Defs/One,
> so, if I'm understanding the import documentation

It should work as you describe it.
Can you just clarify how you are doing this?
Are you setting sys.path in the same program you
are trying to run? Or is it set in a Python startup script?

How do you run a.py? Are you importing it into an existing
Python session(with sys.path set or are you ruinning the
file standalone?

Basically the only thing I can think of is that your setting
of sys.path isn't being seen.

Can you put a debug print statement to display sys.path
just before the import b statement? That way we make
absolutely certain Pythoncan see the folder.

Alan G.



> (http://www.python.org/doc/current/ref/import.html), then the import
statement should be searching the list of directories in
> sys.path, and finding it, but instead I get the ImportError.
>
> I'm using Python 2.3.4 on Windows XP, using Xemacs to develop it and
debug as I go using the interpreter.  Could this be something
> weird about how xemacs is running the code?
> Thanks,
> Ryan
>



More information about the Tutor mailing list