The Famous Error Message: "ImportError: No module named python_script"

greg greg at cosc.canterbury.ac.nz
Fri Dec 15 01:49:14 EST 2006


rich murphy wrote:
> So, I assumed "the current directory" is C:\Python25 which did not
> work... What directory does it mean then?

It means the current directory of the process running
the Python interpreter, which, unless you've done
something to change it, will be the same as the
current directory of the command shell that you
ran the Python interpreter from. That, in turn,
will be whatever you set it to using the "cd" command.

However, it's important to understand that this only
applies when using the Python interpreter *interactively*,
which means just typing "python" and entering Python
statements at the >>> prompt.

If you tell the interpreter to run a .py file,
e.g.

    python some\dir\myfile.py

then it does *not* look in the current directory of
the process for imported files -- instead, it looks in the
directory containing the "main" .py file, i.e. the one
you passed on the command line. This is more useful,
since it allows you to keep the main file and the
modules it uses together, and not have to worry about
cd'ing to the directory in order to run it.

Also, if you're using an IDE such as IDLE or PythonWin
instead of running the interpreter from a command shell,
relying on the "current directory" isn't very useful,
since it's hard to predict what it will be. Again,
just put the files to be imported alongside your main
.py file and you should be all right.

There are various ways of specifying additional places
to look for imported modules, but that should be enough
to get you going.

--
Greg



More information about the Python-list mailing list