Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch python 'ImportError' if import from source directory

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?

like image 613
Julian Avatar asked Feb 07 '13 12:02

Julian


People also ask

How do you call a module from another directory in Python?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.

How do I fix the ImportError No module named error in Python?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

How do I resolve ImportError?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .


1 Answers

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.

like image 103
isedev Avatar answered Sep 23 '22 18:09

isedev