pytest_pythonpath.py 845 B

1234567891011121314151617181920212223
  1. import sys
  2. import os
  3. import site
  4. import pytest
  5. def pytest_addoption(parser):
  6. # py.test has an issue where the cwd is not in the PYTHONPATH. Fix it here.
  7. if os.getcwd() not in sys.path:
  8. sys.path.insert(0, os.getcwd())
  9. parser.addini("python_paths", type="pathlist", help="space seperated directory paths to add to PYTHONPATH via sys.path.insert(0, path)",
  10. default=[])
  11. parser.addini("site_dirs", type="pathlist", help="space seperated directory paths to add to PYTHONPATH via site.addsitedir(path)",
  12. default=[])
  13. @pytest.mark.tryfirst
  14. def pytest_load_initial_conftests(args, early_config, parser):
  15. for path in reversed(early_config.getini("python_paths")):
  16. sys.path.insert(0, str(path))
  17. for path in early_config.getini("site_dirs"):
  18. site.addsitedir(str(path))