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

# 환경 변수

> CWSandbox 인스턴스에서 세션 수준 기본값을 포함한 환경 변수를 설정하고 관리하는 방법을 알아봅니다

<Note>
  The examples on this page omit `auth`. To authenticate with your W\&B API key, install `cwsandbox[wandb]` and pass `auth=AuthStrategy.WANDB` to `Sandbox.run()`, `Sandbox()`, or `Sandbox.session()`. See [Get started](/products/sandboxes/serverless/get-started).
</Note>

This guide shows you how to set and manage environment variables in CWSandbox instances. Use environment variables to pass non-sensitive configuration values, such as log levels or project identifiers, into your sandboxes. You can set them on individual sandboxes, share them across sandboxes in a session, or scope them to remote functions.

<Warning>
  Don't hardcode API keys, passwords, or other credentials in `environment_variables`, or print them to logs. Prefer secret references over passing raw credentials. Keep sensitive values in a secret manager, even when W\&B secret injection isn't available. This guidance applies to both serverless and CKS sandboxes.
</Warning>

W\&B로 인증하는 서버리스 샌드박스에서는 [W\&B 시크릿](/ko/products/sandboxes/serverless/secrets)을 사용하세요. 서버가 이 참조를 해석해 해당 값을 환경 변수로 주입합니다. W\&B 시크릿 주입은 CKS 배치를 사용하거나 CoreWeave API 액세스 토큰으로 인증하는 샌드박스에서는 사용할 수 없습니다. CKS에서는 조직에서 승인한 방법으로 워크로드에 시크릿을 전달하세요.

## Basic usage

To make values available to processes running inside the sandbox, pass an `environment_variables` dictionary to `Sandbox.run`:

```python theme={"system"}
from cwsandbox import Sandbox

with Sandbox.run(
    environment_variables={"LOG_LEVEL": "info"},
) as sandbox:
    result = sandbox.exec([
        "python",
        "-c",
        "import os; print(os.environ.get('LOG_LEVEL'));",
    ]).result()
    print(result.stdout.strip())  # "info"
```

## Session-level defaults

When you need the same variables in several sandboxes, define them once on a session instead of repeating them per sandbox. Individual sandboxes can override session defaults or add their own variables:

```python theme={"system"}
from cwsandbox import SandboxDefaults, Session

defaults = SandboxDefaults(
    environment_variables={
        "PROJECT_ID": "my-project",
        "LOG_LEVEL": "info",
    },
)

with Session(defaults) as session:
    with session.sandbox() as sb1:
        result = sb1.exec([
            "python",
            "-c",
            "import os; print(os.environ.get('LOG_LEVEL'));",
        ]).result()
        print(result.stdout.strip())  # "info"

    # Override LOG_LEVEL and add new variable
    with session.sandbox(
        environment_variables={
            "LOG_LEVEL": "debug",  # Override session default
            "MODEL_NAME": "gpt-4",  # Add new variable
        }
    ) as sb2:
        result = sb2.exec([
            "python",
            "-c",
            "import os; "
            "print(os.environ.get('PROJECT_ID')); "
            "print(os.environ.get('LOG_LEVEL')); "
            "print(os.environ.get('MODEL_NAME'));",
        ]).result()
        lines = result.stdout.strip().split("\n")
        print(lines)  # ["my-project", "debug", "gpt-4"]
```

## Remote functions

Remote functions inherit environment variables from the session and can declare their own through the `@session.function` decorator. The SDK merges function-level variables with session defaults at call time:

```python theme={"system"}
with Session(defaults) as session:
    @session.function(environment_variables={"MODEL_VERSION": "v2.0"})
    def process(task_id: int) -> dict:
        import os
        return {
            "task": task_id,
            "project": os.environ.get("PROJECT_ID"),  # From session defaults
            "version": os.environ.get("MODEL_VERSION"),  # From function decorator
        }

    result = process.remote(42).result()
    print(result)  # {"task": 42, "project": "my-project", "version": "v2.0"}
```

<Warning>
  Environment variables are passed by reference. Subsequent function calls reflect mutations to the dictionary after decoration, as shown in the following example.
</Warning>

```python theme={"system"}
env_vars = {"MODEL_VERSION": "v2.0"}

@session.function(environment_variables=env_vars)
def process(task_id: int) -> dict:
    import os
    return {"version": os.environ.get("MODEL_VERSION")}

result = process.remote(42).result()  # version: "v2.0"

env_vars["MODEL_VERSION"] = "v3.0"  # Mutate the dictionary

result = process.remote(42).result()  # version: "v3.0" (changed)
```
