MCPcopy Create free account
hub / github.com/pytest-dev/pytest / syspath_prepend

Method syspath_prepend

src/_pytest/monkeypatch.py:313–336  ·  view source on GitHub ↗

Prepend ``path`` to ``sys.path`` list of import locations.

(self, path)

Source from the content-addressed store, hash-verified

311 self.delitem(environ, name, raising=raising)
312
313 def syspath_prepend(self, path) -> None:
314 """Prepend ``path`` to ``sys.path`` list of import locations."""
315
316 if self._savesyspath is None:
317 self._savesyspath = sys.path[:]
318 sys.path.insert(0, str(path))
319
320 # https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
321 # this is only needed when pkg_resources was already loaded by the namespace package
322 if "pkg_resources" in sys.modules:
323 from pkg_resources import fixup_namespace_packages
324
325 fixup_namespace_packages(str(path))
326
327 # A call to syspathinsert() usually means that the caller wants to
328 # import some dynamically created files, thus with python3 we
329 # invalidate its import caches.
330 # This is especially important when any namespace package is in use,
331 # since then the mtime based FileFinder cache (that gets created in
332 # this case already) gets not invalidated when writing the new files
333 # quickly afterwards.
334 from importlib import invalidate_caches
335
336 invalidate_caches()
337
338 def chdir(self, path: Union[str, "os.PathLike[str]"]) -> None:
339 """Change the current working directory to the specified path.

Calls 1

insertMethod · 0.80