DeprecationWarning
DeprecationWarning: function X is deprecated
Traceback
Traceback (most recent call last):
File "main.py", line 3, in <module>
pass
DeprecationWarning: function X is deprecatedWhat causes this error
A deprecated feature or API was used that will be removed in a future version. The warning provides advance notice to update your code.
How to fix it
Read the warning message for the recommended replacement. Update code to use the new API. Use `python -W default::DeprecationWarning` to see all warnings. Pin dependency versions until migration is complete.
Code that causes this error
import collections
class MyDict(collections.MutableMapping): # deprecated
passFixed code
import collections.abc
class MyDict(collections.abc.MutableMapping):
passAbout DeprecationWarning
A DeprecationWarning is issued when a feature is being used that is scheduled for removal in a future version of Python or a library. These warnings serve as early notifications to developers to update their code before the feature is actually removed. By default, DeprecationWarning is ignored in normal code but shown in test suites and development environments.
To see them in production code, use `python -W default::DeprecationWarning` or configure the `warnings` module. Common examples include using `collections.MutableMapping` instead of `collections.abc.MutableMapping`, using deprecated `distutils` instead of `setuptools`, and calling functions with deprecated parameter names. Libraries typically provide a migration path alongside the deprecation notice.
Addressing deprecation warnings proactively prevents breaking changes when upgrading dependencies or Python itself.
Common scenarios
Using deprecated APIs that will be removed in future versions
Performing numerical operations that produce inf or NaN values
Leaving files, sockets, or connections open without proper cleanup
Calling async functions without awaiting the result