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)