From 73378552d82b3bd3760ccb11894c870ee8989bdd Mon Sep 17 00:00:00 2001 From: Daraan Date: Fri, 3 May 2024 12:42:52 +0200 Subject: [PATCH] Backward compatible upgrade to fix deprecation warnings concerning LooseVersion and pkg_resources for Python3.8+ (#1058) * upgraded version checking to fix deprecation warnings Removed usage of pgk_resources and distutils.version.LooseVersion * < python3.8 backward compatible version checking * Compacter version checking. --------- Co-authored-by: glopezdiest <58212725+glopezdiest@users.noreply.github.com> --- scenario_runner.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/scenario_runner.py b/scenario_runner.py index b31c53d09..89039758c 100755 --- a/scenario_runner.py +++ b/scenario_runner.py @@ -20,7 +20,10 @@ import argparse from argparse import RawTextHelpFormatter from datetime import datetime -from distutils.version import LooseVersion +try: + from packaging.version import Version +except ImportError: + from distutils.version import LooseVersion as Version # Python 2 fallback import importlib import inspect import os @@ -28,7 +31,17 @@ import sys import time import json -import pkg_resources + +try: + # requires Python 3.8+ + from importlib.metadata import metadata + def get_carla_version(): + return Version(metadata("carla")["Version"]) +except ModuleNotFoundError: + # backport checking for older Python versions; module is deprecated + import pkg_resources + def get_carla_version(): + return Version(pkg_resources.get_distribution("carla").version) import carla @@ -46,6 +59,9 @@ # Version of scenario_runner VERSION = '0.9.13' +# Minimum version of CARLA that is required +MIN_CARLA_VERSION = '0.9.15' + class ScenarioRunner(object): @@ -91,9 +107,9 @@ def __init__(self, args): # requests in the localhost at port 2000. self.client = carla.Client(args.host, int(args.port)) self.client.set_timeout(self.client_timeout) - dist = pkg_resources.get_distribution("carla") - if LooseVersion(dist.version) < LooseVersion('0.9.15'): - raise ImportError("CARLA version 0.9.15 or newer required. CARLA version found: {}".format(dist)) + carla_version = get_carla_version() + if carla_version < Version(MIN_CARLA_VERSION): + raise ImportError("CARLA version {} or newer required. CARLA version found: {}".format(MIN_CARLA_VERSION, carla_version)) # Load agent if requested via command line args # If something goes wrong an exception will be thrown by importlib (ok here)