Catch python ‘ImportError’ if import from source directory [duplicate]

ImportError: No module named foo actually means the module foo.py or package foo/__init__.py could not be found in any of the directories in the search path (sys.path list).

Since sys.path usually contains . (the current directory), that’s probably what you meant by being in the source directory. You are in the top-level directory of package foo (where the __init__.py file is) so obviously you can’t find foo/__init__.py.

Finally, you’ve answered your own question, more or less:

try:
    from _foo import *
except ImportError:
    raise ImportError('<any message you want here>')

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 the PYTHONPATH environment variable (on Unix) to allow you to run from your source directory. Might even work on Windows, but I wouldn’t know.

Leave a Comment