How to declare a module deprecated in python?
I want a warning to be printed whenever a particular module is imported or any of its functions are called.
You want to warn with a DeprecationWarning.
Exactly how you call it doesn't matter that much, but the stdlib has a standard pattern for deprecated modules, like this:
# doc string, top-level comments, imports, __all__ =
import warnings
warnings.warn("the spam module is deprecated", DeprecationWarning,
stacklevel=2)
# normal module code
See the 2.7 sets source for an example.
Were you not happy with warnings.warn, to print to your console, you could follow this solution, which uses the logging module for Python.
However, in my experience, you need to add a logging.basicConfig() command before your logging.warning function call, so I'd suggest something like:
import logging
logging.basicConfig()
logging.warning(msg="this is a warning")
warnings, but find this acceptable? Don't use basicConfig in a module; configuration is for the script that uses your module.
print "Deprecated"to__init__or add a decorator that does the same.__init__method?