# Buggy code
def load_locustfile(path):
    """
    Import given locustfile path and return (docstring, callables).

    Specifically, the locustfile's ``__doc__`` attribute (a string) and a
    dictionary of ``{'name': callable}`` containing all callables which pass
    the "is a Locust" test.
    """
    # Get directory and locustfile name
    directory, locustfile = os.path.split(path)
    # If the directory isn't in the PYTHONPATH, add it so our import will work
    added_to_path = False
    index = None
    if directory not in sys.path:
        sys.path.insert(0, directory)
        added_to_path = True
    # If the directory IS in the PYTHONPATH, move it to the front temporarily,
    # otherwise other locustfiles -- like Locusts's own -- may scoop the intended
    # one.
    else:
        i = sys.path.index(directory)
        if i != 0:
            # Store index for later restoration
            index = i
            # Add to front, then remove from original position
            sys.path.insert(0, directory)
            del sys.path[i + 1]
    # Perform the import (trimming off the .py)
    imported = __import__(os.path.splitext(locustfile)[0])
    # Remove directory from path if we added it ourselves (just to be neat)
    if added_to_path:
        del sys.path[0]
    # Put back in original index if we moved it
    if index is not None:
        sys.path.insert(index + 1, directory)
        del sys.path[0]
    # Return our two-tuple
    locusts = dict(filter(is_locust, vars(imported).items()))
    return imported.__doc__, locusts


# Traceback (ImportError)
unuser@miguel-pc:/$ locust -f /tests/login_and_minimal_navigation.test.py -H http://127.0.0.1:10000 --no-web -c 10 -r 2 -t 30s --only-summary
[2019-01-04 09:32:05,137] miguel-pc/ERROR/stderr: Traceback (most recent call last):
[2019-01-04 09:32:05,137] miguel-pc/ERROR/stderr: File "/usr/local/bin/locust", line 11, in <module>
[2019-01-04 09:32:05,137] miguel-pc/ERROR/stderr:
[2019-01-04 09:32:05,137] miguel-pc/ERROR/stderr: sys.exit(main())
[2019-01-04 09:32:05,137] miguel-pc/ERROR/stderr:
[2019-01-04 09:32:05,137] miguel-pc/ERROR/stderr: File "/usr/local/lib/python3.5/site-packages/locust/main.py", line 391, in main
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr:
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr: docstring, locusts = load_locustfile(locustfile)
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr:
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr: File "/usr/local/lib/python3.5/site-packages/locust/main.py", line 358, in load_locustfile
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr:
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr: imported = __import__(os.path.splitext(locustfile)[0])
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr:
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr: ImportError
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr: :
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr: No module named 'login_and_minimal_navigation'
[2019-01-04 09:32:05,138] miguel-pc/ERROR/stderr:
unuser@miguel-pc:/$

# Fixed code
def load_locustfile(path):
    """
    Import given locustfile path and return (docstring, callables).

    Specifically, the locustfile's ``__doc__`` attribute (a string) and a
    dictionary of ``{'name': callable}`` containing all callables which pass
    the "is a Locust" test.
    """

    def __import_locustfile__(filename, path):
        """
        Loads the locust file as a module, similar to performing `import`
        """
        try:
            # Python 3 compatible
            source = importlib.machinery.SourceFileLoader(
                os.path.splitext(locustfile)[0], path
            )
            imported = source.load_module()
        except AttributeError:
            # Python 2.7 compatible
            import imp

            imported = imp.load_source(os.path.splitext(locustfile)[0], path)

        return imported

    # Get directory and locustfile name
    directory, locustfile = os.path.split(path)
    # If the directory isn't in the PYTHONPATH, add it so our import will work
    added_to_path = False
    index = None
    if directory not in sys.path:
        sys.path.insert(0, directory)
        added_to_path = True
    # If the directory IS in the PYTHONPATH, move it to the front temporarily,
    # otherwise other locustfiles -- like Locusts's own -- may scoop the intended
    # one.
    else:
        i = sys.path.index(directory)
        if i != 0:
            # Store index for later restoration
            index = i
            # Add to front, then remove from original position
            sys.path.insert(0, directory)
            del sys.path[i + 1]
    # Perform the import
    imported = __import_locustfile__(locustfile, path)
    # Remove directory from path if we added it ourselves (just to be neat)
    if added_to_path:
        del sys.path[0]
    # Put back in original index if we moved it
    if index is not None:
        sys.path.insert(index + 1, directory)
        del sys.path[0]
    # Return our two-tuple
    locusts = dict(filter(is_locust, vars(imported).items()))
    return imported.__doc__, locusts