MCPcopy
hub / github.com/deepnote/deepnote / buildPythonEnv

Function buildPythonEnv

packages/runtime-core/src/python-env.ts:181–210  ·  view source on GitHub ↗
(
  resolvedPythonPath: string,
  baseEnv: Record<string, string | undefined> = process.env
)

Source from the content-addressed store, hash-verified

179 * @returns Environment variables object for use with child_process.spawn()
180 */
181export async function buildPythonEnv(
182 resolvedPythonPath: string,
183 baseEnv: Record<string, string | undefined> = process.env
184): Promise<Record<string, string | undefined>> {
185 const env = { ...baseEnv }
186
187 if (isBareSystemPython(resolvedPythonPath)) {
188 return env
189 }
190
191 const pythonDir = dirname(resolve(resolvedPythonPath))
192
193 // Prepend the Python's directory to PATH so subprocesses (e.g. Jupyter kernels)
194 // find the correct Python when using bare 'python' commands
195 const pathKey = Object.keys(env).find(k => k.toLowerCase() === 'path') || 'PATH'
196 const currentPath = env[pathKey] || ''
197 env[pathKey] = currentPath ? `${pythonDir}${delimiter}${currentPath}` : pythonDir
198
199 // Detect if this Python is inside a virtual environment
200 const venvRoot = await detectVenvRoot(resolve(resolvedPythonPath))
201 if (venvRoot) {
202 env.VIRTUAL_ENV = venvRoot
203 } else {
204 // Clear any inherited VIRTUAL_ENV to prevent the current shell's venv
205 // from interfering with the specified Python
206 delete env.VIRTUAL_ENV
207 }
208
209 return env
210}

Callers 2

python-env.test.tsFile · 0.90
startServerFunction · 0.90

Calls 2

isBareSystemPythonFunction · 0.85
detectVenvRootFunction · 0.85

Tested by

no test coverage detected