20
votes

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.

1
Add print "Deprecated" to __init__ or add a decorator that does the same. - ForceBru
@ForceBru: How are you going to add a decorator to a module? And where are you going to find its __init__ method? - abarnert

1 Answers

21
votes

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.