백지현
백지현

Reputation: 21

pkg_resources is deprecated as an API - Python

When I try to "ros2 topic list" or "ros2 bag play - ", while making warnings into errors:

I get this error: /opt/ros/foxy/bin/ros2:6: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html

However, it works normally after receiving the error message. Can I ignore this error message?

Upvotes: 2

Views: 6059

Answers (1)

Nimit Gupta
Nimit Gupta

Reputation: 141

Yes, You can ignore the Deprecation Warning using python warnings module.

Steps 1 - Create class as a decorator

class IgnoreWarnings:
    def __init__(self, function):
        self.function = function 
    def __call__(self, *args, **kwargs):
        with contextlib.suppress(IndexError, ValueError, CustomError):
            with warnings.catch_warnings(record = True):
                for _category in [Warning, SyntaxWarning, RuntimeWarning, FutureWarning, DeprecationWarning, PendingDeprecationWarning]:
                    warnings.filterwarnings(action = "ignore", category = _category)
                return self.function(*args, **kwargs)

Step 2 - Use the decorator

class UpdatePythonPackages(CreateLogger):
    @classmethod
    @IgnoreWarnings
    def update_python_packages(cls):
        logger = super(UpdatePythonPackages).__get__(cls, None).create_logger()
        import pkg_resources; subprocess.check_call("python -m pip install --upgrade  " + ' '.join([dist.project_name for dist in pkg_resources.working_set]), shell = True)
        return None

By doing this you can ignore the pkg_resources deprecation warning.

Upvotes: 0

Related Questions