Skip to content

Commit

Permalink
Backward compatible upgrade to fix deprecation warnings concerning Lo…
Browse files Browse the repository at this point in the history
…oseVersion 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 <[email protected]>
  • Loading branch information
Daraan and glopezdiest committed May 3, 2024
1 parent b1ce704 commit 7337855
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions scenario_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,28 @@
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
import signal
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

Expand All @@ -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):

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7337855

Please sign in to comment.