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

Function module_name_from_path

src/_pytest/pathlib.py:572–590  ·  view source on GitHub ↗

Return a dotted module name based on the given path, anchored on root. For example: path="projects/src/tests/test_foo.py" and root="/projects", the resulting module name will be "src.tests.test_foo".

(path: Path, root: Path)

Source from the content-addressed store, hash-verified

570
571
572def module_name_from_path(path: Path, root: Path) -> str:
573 """
574 Return a dotted module name based on the given path, anchored on root.
575
576 For example: path="projects/src/tests/test_foo.py" and root="/projects", the
577 resulting module name will be "src.tests.test_foo".
578 """
579 path = path.with_suffix("")
580 try:
581 relative_path = path.relative_to(root)
582 except ValueError:
583 # If we can't get a relative path to root, use the full path, except
584 # for the first part ("d:\\" or "/" depending on the platform, for example).
585 path_parts = path.parts[1:]
586 else:
587 # Use the parts for the relative path to the root path.
588 path_parts = relative_path.parts
589
590 return ".".join(path_parts)
591
592
593def insert_missing_modules(modules: Dict[str, ModuleType], module_name: str) -> None:

Callers 2

import_pathFunction · 0.85

Calls

no outgoing calls

Tested by 1