Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/8503.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`pytest.MonkeyPatch.syspath_prepend` no longer fails when
``setuptools`` is not installed.
It now only calls :func:`pkg_resources.fixup_namespace_packages` if
``pkg_resources`` was previously imported, because it is not needed otherwise.
7 changes: 5 additions & 2 deletions src/_pytest/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,17 @@ def delenv(self, name: str, raising: bool = True) -> None:

def syspath_prepend(self, path) -> None:
"""Prepend ``path`` to ``sys.path`` list of import locations."""
from pkg_resources import fixup_namespace_packages

if self._savesyspath is None:
self._savesyspath = sys.path[:]
sys.path.insert(0, str(path))

# https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
fixup_namespace_packages(str(path))
# this is only needed when pkg_resources was already loaded by the namespace package
if "pkg_resources" in sys.modules:
from pkg_resources import fixup_namespace_packages

fixup_namespace_packages(str(path))

# A call to syspathinsert() usually means that the caller wants to
# import some dynamically created files, thus with python3 we
Expand Down