> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ns.rocks/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Run Python code interpreter cells with persistent state and rich outputs.

Python is the default code interpreter language on the standard
`code-interpreter` template.

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  result = machine.run_code("""
  import numpy as np

  values = np.array([1, 2, 3])
  values.mean()
  """, language="python")

  print(result.results[0].text)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  const result = await machine.code.run(`
  import numpy as np

  values = np.array([1, 2, 3])
  values.mean()
  `, { language: "python" });

  console.log(result.results[0].text);
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code run mch_123 "import numpy as np

  values = np.array([1, 2, 3])
  values.mean()" --language python
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # returns a Server-Sent Events stream of JSON events
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "import numpy as np\nvalues = np.array([1, 2, 3])\nvalues.mean()", "language": "python"}'
  ```
</CodeGroup>

Use a Python context when later cells should reuse imports and variables:

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  ctx = machine.code_interpreter.create_code_context(language="python")
  machine.code_interpreter.run_code("import pandas as pd", context=ctx)
  machine.code_interpreter.run_code("df = pd.DataFrame({'x': [1, 2, 3]})", context=ctx)
  result = machine.code_interpreter.run_code("df['x'].sum()", context=ctx)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  const ctx = await machine.code.createContext({ language: "python" });
  await machine.code.run("import pandas as pd", { context: ctx });
  await machine.code.run("df = pd.DataFrame({'x': [1, 2, 3]})", { context: ctx });
  const result = await machine.code.run("df['x'].sum()", { context: ctx });
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code context create mch_123 --language python
  nullspace machine code run mch_123 "import pandas as pd" --context <context-id>
  nullspace machine code run mch_123 "df = pd.DataFrame({'x': [1, 2, 3]})" --context <context-id>
  nullspace machine code run mch_123 "df['x'].sum()" --context <context-id>
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" -d '{"language": "python"}'
  # then run each cell against the returned context id (SSE stream)
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "import pandas as pd", "context_id": "<context-id>"}'
  ```
</CodeGroup>

Install additional Python packages into the running machine with
`install_packages()`.

## Related

* [Pre-installed libraries](./pre-installed-libraries)
* [Code contexts](./contexts)
