MCPcopy Create free account
hub / github.com/ashleve/rootutils / set_root

Function set_root

rootutils/rootutils.py:75–112  ·  view source on GitHub ↗

Set given path as a project root. Args: path (Union[str, Path]): Project root path. project_root_env_var (bool, optional): Whether to set PROJECT_ROOT environment variable. dotenv (bool, optional): Whether to load `.env` file from project root. pythonpath (bool,

(
    path: Union[str, Path],
    project_root_env_var: bool = True,
    dotenv: bool = True,
    pythonpath: bool = False,
    cwd: bool = False,
)

Source from the content-addressed store, hash-verified

73
74
75def set_root(
76 path: Union[str, Path],
77 project_root_env_var: bool = True,
78 dotenv: bool = True,
79 pythonpath: bool = False,
80 cwd: bool = False,
81) -> None:
82 """Set given path as a project root.
83
84 Args:
85 path (Union[str, Path]): Project root path.
86 project_root_env_var (bool, optional): Whether to set PROJECT_ROOT environment variable.
87 dotenv (bool, optional): Whether to load `.env` file from project root.
88 pythonpath (bool, optional): Whether to add project root to pythonpath.
89 cwd (bool, optional): Whether to set current working directory to project root.
90
91 Raises:
92 FileNotFoundError: If root path does not exist.
93
94 Returns:
95 None
96 """
97 path = str(path)
98
99 if not os.path.exists(path):
100 raise FileNotFoundError(f"Project root path does not exist: {path}")
101
102 if project_root_env_var:
103 os.environ["PROJECT_ROOT"] = path
104
105 if dotenv:
106 load_dotenv(os.path.join(path, ".env"))
107
108 if pythonpath:
109 sys.path.insert(0, path)
110
111 if cwd:
112 os.chdir(path)
113
114
115def setup_root(

Callers 2

test_set_rootFunction · 0.90
setup_rootFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_set_rootFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…