When one tries to import a module foo while being in the source directory, one gets an rather confusing ImportError message: ImportError: No module named foo.
How can I easily catch this case and return a more informative message, e.g. ‘Please do not load module foo from the source directory’?
Having the __init__.py, I would start with:
try:
from _foo import *
except ImportError:
## check whether in the source directory...
So I would like to distinguish the different causes for an ImportError (e.g. because a module named foo is not installed at all), and detect the case in which the setup.py is located in the current directory. What would be a elegant way of doing this?
ImportError: No module named fooactually means the modulefoo.pyor packagefoo/__init__.pycould not be found in any of the directories in the search path (sys.pathlist).Since
sys.pathusually contains.(the current directory), that’s probably what you meant by being in the source directory. You are in the top-level directory of packagefoo(where the__init__.pyfile is) so obviously you can’t findfoo/__init__.py.Finally, you’ve answered your own question, more or less:
Alternatively, you could check the contents of
sys.path, the current directory and, if known, the expected package directory and produce an even detailed and context-aware message.Or add
..to thePYTHONPATHenvironment variable (on Unix) to allow you to run from your source directory. Might even work on Windows, but I wouldn’t know.