I have a module that depends on some system settings. For example, to work properly it needs to have an environment variable FOO set. I would like the module to raise an Exception if this condition is not met at import time.
# mymodule.py
if 'FOO' not in sys.environ:
raise SomeException('ensure that FOO is provided')
I would like to know:
class MyModuleImportError(Exception)
or maybe some built-in exceptions are more suitable and commonly used? One candidate would be ImportError , but it seems to be reserved for situation when python "fails to find the module definition" Is this what you are looking for?
>>> import os
>>> if not os.getenv('FOO', False):
raise OSError('FOO not in envs')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.