Python Error Handling Module
Contents |
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as python exception class parsing errors, are perhaps the most common kind of complaint you get python exception message while you are still learning Python: >>> while True print('Hello world') File "
Python Print Exception
it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here: >>> 10 * (1/0) Traceback (most recent call last): File "
This module never needs to be imported explicitly: the exceptions are provided in the built-in namespace as well as the class="pre">exceptionsSyntax For Generic Except Clause In Python
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn http://stackoverflow.com/questions/3131217/error-handling-when-importing-modules more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags http://stackoverflow.com/questions/14750711/catch-python-importerror-if-import-from-source-directory Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error handling when importing modules up vote 14 down vote favorite 2 This probably has an obvious answer, but I'm a beginner. I've got a "module" python exception (really just a file with a bunch of functions I often use) at the beginning of which I import a number of other modules. Because I work on many systems, however, not all modules may be able to load on any particular machine. To make things slightly more difficult, I also change the names of the packages when I import them -- for example, matplotlib gets abbreviated to mp. What I'd like to do is only python error handling load those modules that exist on the system I'm currently using, and do some error handling on the ones that don't. The only way I can think of doing so is by enclosing each import statement inside its own try block, which seems pretty un-pythonic. If I enclose them all in the same try block, whichever module throws an error will prevent the subsequent modules from being loaded. Any ideas that might make things look prettier? It would be so easy if I didn't want to change their names... Thanks! Dave python error-handling module cross-platform share|improve this question edited Aug 5 '14 at 20:46 smci 7,55043878 asked Jun 28 '10 at 9:15 Dave Schultz 71113 2 the "imp" module should help you. you might want to read this: docs.python.org/library/imp.html –mawimawi Jun 28 '10 at 9:25 @mawimawi : How? Please explain and expand your comment into a proper answer? (I read the doc and it's not obvious why we would want to use that module any more than any other approach) –smci Aug 5 '14 at 20:48 add a comment| 2 Answers 2 active oldest votes up vote 17 down vote I don't think try except block is un-pythonic; instead it's a common way to handle import on Python. Quoting Dive into Python: There are a lot of other uses for exceptions besides handl
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Catch python 'ImportError' if import from source directory up vote 10 down vote favorite 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? python exception-handling importerror share|improve this question edited Feb 7 '13 at 13:42 Paolo 7,09495088 asked Feb 7 '13 at 12:05 Julian 1691110 add a comment| 1 Answer 1 active oldest votes up vote 8 down vote accepted 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('