# Buggy code
def _configure_syspath():
    sys.path.insert(1, _lib_location())
    sys.path.insert(1, _ext_lib_location())


# Traceback (ImportError)
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/medusa/scene_exceptions.py", line 13, in <module>
Sep 07 08:07:56 norm env[30534]:     import adba
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/ext/adba/__init__.py", line 27, in <module>
Sep 07 08:07:56 norm env[30534]:     from configparser import ConfigParser
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/ext/configparser.py", line 12, in <module>
Sep 07 08:07:56 norm env[30534]:     from backports.configparser import (
Sep 07 08:07:56 norm env[30534]: ImportError: No module named configparser
Sep 07 08:07:56 norm systemd[882]: medusa.service: Control process exited, code=exited status=1
Sep 07 08:07:56 norm systemd[882]: medusa.service: Failed with result 'exit-code'.
Sep 07 08:07:56 norm systemd[882]: Failed to start Medusa user service.


Sep 07 08:07:56 norm env[30534]: Traceback (most recent call last):
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/start.py", line 6, in <module>
Sep 07 08:07:56 norm env[30534]:     from medusa.__main__ import main
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/medusa/__main__.py", line 67, in <module>
Sep 07 08:07:56 norm env[30534]:     from medusa import (
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/medusa/name_cache.py", line 12, in <module>
Sep 07 08:07:56 norm env[30534]:     from medusa.scene_exceptions import (
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/medusa/scene_exceptions.py", line 13, in <module>
Sep 07 08:07:56 norm env[30534]:     import adba
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/ext/adba/__init__.py", line 27, in <module>
Sep 07 08:07:56 norm env[30534]:     from configparser import ConfigParser
Sep 07 08:07:56 norm env[30534]:   File "/opt/Medusa/ext/configparser.py", line 12, in <module>
Sep 07 08:07:56 norm env[30534]:     from backports.configparser import (
Sep 07 08:07:56 norm env[30534]: ImportError: No module named configparser
Sep 07 08:07:56 norm systemd[882]: medusa.service: Control process exited, code=exited status=1
Sep 07 08:07:56 norm systemd[882]: medusa.service: Failed with result 'exit-code'.

# Fixed code
def _configure_syspath():
    """Add the vendored libraries into `sys.path`."""
    # Note: These paths will be inserted into `sys.path` in reverse order (LIFO)
    # So the last path on this list will be inserted as the first path on `sys.path`
    # right after the current working dir.
    # For example: [ cwd, pathN, ..., path1, path0, <rest_of_sys.path> ]

    paths_to_insert = [_lib_location(), _ext_lib_location()]

    if sys.version_info[0] == 2:
        # Add Python 2-only vendored libraries
        paths_to_insert.extend(
            [
                # path_to_lib2,
                # path_to_ext2
            ]
        )
    elif sys.version_info[0] == 3:
        # Add Python 3-only vendored libraries
        paths_to_insert.extend(
            [
                # path_to_lib3,
                # path_to_ext3
            ]
        )

    # Insert paths into `sys.path` and handle `.pth` files
    # Inspired by: https://bugs.python.org/issue7744
    for dirpath in paths_to_insert:
        # Clear `sys.path`
        sys.path, remainder = sys.path[:1], sys.path[1:]
        # Add directory as a site-packages directory and handle `.pth` files
        site.addsitedir(dirpath)
        # Restore rest of `sys.path`
        sys.path.extend(remainder)