Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appropriate Python Exception Class for Missing Settings File

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.

like image 500
jdg Avatar asked Jan 13 '13 02:01

jdg


1 Answers

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
like image 191
Raymond Hettinger Avatar answered Nov 15 '22 15:11

Raymond Hettinger