12

What is the appropriate python Exception to raise if there is a missing settings file?

For example, in Django projects, a light-weight way to allow users to define local settings is to add the following snippet to the settings.py file

try:
    from local_settings import *
except ImportError:
    # want to add informative Exception here
    pass

thus, any local settings override the defaults in settings.py.

2
  • 1
    Maybe EnvironmentError or LookupError or even RuntimeError. But why is it important? You can create your own if you care: class MyCustomErrorThing(Exception): pass
    – JBernardo
    Commented Jan 13, 2013 at 2:55
  • That is actually what I am doing at the moment, although I was curious if there was a "better" way.
    – jdg
    Commented Jan 13, 2013 at 3:02

1 Answer 1

5

The usual exception for missing files is IOError.

You can customize the wording by creating a subclass:

class MissingSettingsFile(IOError):
    'Missing local settings file'
    pass

Then, use that custom exception in your code snippet:

try:
    from local_settings import *
except ImportError:
    raise MissingSettingsFile

Not the answer you're looking for? Browse other questions tagged or ask your own question.