[Tutor] Do not understand why test is running.
steve at pearwood.info
Sun Aug 23 18:37:06 CEST 2015
On Sun, Aug 23, 2015 at 10:42:25AM -0500, boB Stepp wrote:
> If I try this or begin in E:\Projects\mcm and type py
> ./test/db/test_manager.py I get
> E:\Projects\mcm>py ./test/db/test_manager.py
> Traceback (most recent call last):
> File "./test/db/test_manager.py", line 16, in <module>
> import mcm.db.manager
> ImportError: No module named 'mcm'
> I don't understand why this is the case.
Without seeing the complete structure of your project, I cannot be sure,
but I can think of at least two problems:
1) Does the mcm directory contain a file called __init__.py? If not,
then Python will not recognise it as a module and `import mcm` will
2) Even if mcm contains a __init__.py file, the directory itself must be
located in your PYTHONPATH. If you run this:
py -c "import sys; print(sys.path)"
it will show you the directories in the PYTHONPATH that are searched for
a module called mcm. Given that your directory is E:\Projects\mcm, that
implies that E:\Projects\ must be in the search path in order to locate
mcm as a module.
Think of it this way... simplified (very simplified!) Python does this
when you try to import a module:
# `import spam` pseudo-code
for directory in sys.path:
filenames = os.listdir(directory)
for name in filenames:
if os.path.isfile(name) and name == "spam.py":
elif os.path.isdir(name) and name == "spam":
There's more, of course -- the real import code is much more complex, it
handles cached modules in sys.modules, compiled .pyc and .pyo files, zip
files, custom importers, and much more. And I daresay it is more
efficient than the pseudo-code I show above.
But the important factor is that none of the directories in the
PYTHONPATH are themselves considered modules, only their contents can be
considered modules. So, if the current directory is
then that directory will be added to the PYTHONPATH, sure enough. But
there is nothing inside that directory called mcm, you need either:
in order to import it. Or you can add E:\Projects to the PYTHONPATH. Or
move up a level, to E:\Projects, and run your code from there.
To put it another way, Python will look *inside* the current directory
for modules to import, but it won't look *at* the current directory as a
module to import.
More information about the Tutor