1 import sys

2

3 import pytest

4 from pytest import Config

5 from pytest import Parser

6

7

8 def pytest_addoption(parser: Parser) -> None:

9 parser.addini("pythonpath", type="paths", help="Add paths to sys.path", default=[])

10

11

12@pytest.hookimpl(tryfirst=True)

13 def pytest_load_initial_conftests(early_config: Config) -> None:

14 # `pythonpath = a b` will set `sys.path` to `[a, b, x, y, z, ...]`

15 for path in reversed(early_config.getini("pythonpath")):

16 sys.path.insert(0, str(path))

17

18

19@pytest.hookimpl(trylast=True)

20 def pytest_unconfigure(config: Config) -> None:

21 for path in config.getini("pythonpath"):

22 path_str = str(path)

23 if path_str in sys.path:

24 sys.path.remove(path_str)