Python Import Error Exception
Contents |
a try statement with an except clause that mentions a particular class, python exceptions list that clause also handles any exception classes derived from that python custom exception class (but not exception classes from which it is derived). Two exception classes that are not python filenotfounderror related via subclassing are never equivalent, even if they have the same name. The built-in exceptions listed below can be generated by the interpreter or python exception message built-in functions. Except where mentioned, they have an "associated value" indicating the detailed cause of the error. This may be a string or a tuple of several items of information (e.g., an error code and a string explaining the code). The associated value is usually passed as arguments to the exception
Python Valueerror Example
class's constructor. User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition "just like" the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error. The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions. When raising (or re-raising) an exception in an except or finally clause __context__ is automatically set to the last exception caught; if the new exception is not handled the traceback that is eventually displayed will include the originating exception(s) and the final exception. When raising a new exception (rather than using a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
Python Exception Class Methods
and policies of this site About Us Learn more about Stack Overflow python errno the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation python attributeerror object has no attribute 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; https://docs.python.org/3/library/exceptions.html 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, http://stackoverflow.com/questions/14750711/catch-python-importerror-if-import-from-source-directory 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 obviousl
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 http://stackoverflow.com/questions/1618762/friendlier-error-messages-on-import-for-missing-modules Stack Overflow the company Business Learn more about hiring developers or posting ads http://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x 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 Friendlier error messages on import for missing modules up vote 4 down vote python exception favorite I want to implement some friendlier error messages if the user tries to run a python script which tries to import modules that have not been installed. This includes printing out instructions on how to install the missing module. One way to do this would be to put a try..catch block around the imports, but this is a bit ugly since it would turn something simple like import some_module python import error into try: import some_module except ImportError, e: handle_error(e) and it would have to be added to every file. Additionally, ImportError doesn't seem to store the name of the missing module anywhere (except for in the message) so you would have to put a separate try..catch around each import if you need to know the name (like I do). Parsing the name of the module is not the option since the message carried by ImportError might change for python version to version and depending on the user's locale. I guess I could use sys.excepthook to catch all exceptions and pass those except ImportError along. Or would it be possible to define something like safe_import some_module that would behave like I want? Does anyone know of any existing solutions to this problem? python exception-handling share|improve this question asked Oct 24 '09 at 18:57 pafcu 2,23432444 add a comment| 4 Answers 4 active oldest votes up vote 2 down vote accepted I would put additional modules into the package which, when imported, print out the more helpful message, and then raise a regular ImportError. When the true module is installed, your modules will get shadowed (make sure you add the directory where they live at the end of sys.path). share|im
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 ImportError: Cannot import name X up vote 158 down vote favorite 29 I have four different files named: main, vector, entity, and physics. I will not post all the code, mostly just the imports, because I think that's where the error is. But if you want, I can post more. Main: import time from entity import Ent from vector import Vect #the rest just creates an entity and prints the result of movement Entity: from vector import Vect from physics import Physics class Ent: #holds vector information and id def tick(self, dt): #this is where physics changes the velocity and position vectors Vector: from math import * class Vect: #holds i, j, k, and does vector math Physics: from entity import Ent class Physics: #physics class gets an entity and does physics calculations on it. I then run from main.py and I get the following error: Traceback (most recent call last): File "main.py", line 2, in