can1357/oh-my-pi · warning · ValueError
Usage: %set_env KEY VALUE
Error message
Usage: %set_env KEY VALUE
What it means
The %set_env line magic sets an OS environment variable for the eval session. It requires exactly KEY VALUE arguments; otherwise it raises ValueError with a usage string. It performs no other validation of key or value.
Source
Thrown at packages/coding-agent/src/eval/py/runner.py:655
@line_magic("env")
def _magic_env(args: str) -> Any:
args = args.strip()
if not args:
return dict(sorted(os.environ.items()))
if "=" in args:
key, value = args.split("=", 1)
os.environ[key.strip()] = value.strip()
return value.strip()
return os.environ.get(args)
@line_magic("set_env")
def _magic_set_env(args: str) -> str:
parts = args.split(None, 1)
if len(parts) != 2:
raise ValueError("Usage: %set_env KEY VALUE")
key, value = parts
os.environ[key] = value
return value
@line_magic("time")
def _magic_time(args: str) -> Any:
start = time.perf_counter()
result = eval(args, _STATE.user_ns)
elapsed = time.perf_counter() - start
sys.stdout.write(f"Wall time: {elapsed * 1000:.2f} ms\n")
_emit_status("time", elapsed_ms=round(elapsed * 1000, 3))
return result
@line_magic("timeit")
def _magic_timeit(args: str) -> None:
import timeit as _timeitView on GitHub (pinned to 9690622007)
Solutions
- Use space-separated form: %set_env KEY VALUE
- Do not use KEY=VALUE syntax — split(None,1) will not parse it into two parts
- Quote the value if it contains spaces: %set_env KEY "some value"
Example fix
// before %set_env API_KEY=abc123 // after %set_env API_KEY abc123
Defensive patterns
Strategy: validation
Validate before calling
parts = args.split(None, 1)
if len(parts) != 2:
raise ValueError("Usage: %set_env KEY VALUE") # pre-check mirrors the magic Try / catch
try:
%set_env KEY VALUE
except ValueError as e:
if "Usage: %set_env" in str(e):
# rewrite KEY=VALUE form
key, _, value = args.partition('=')
%set_env KEY VALUE Prevention
- Always use space-separated KEY VALUE, never KEY=VALUE
- Quote values containing spaces
- Remember env changes only affect this session
When it happens
Trigger: Running %set_env with zero or one argument — e.g. '%set_env' alone, '%set_env KEY' without a value, or '%set_env KEY=VALUE' (the = form is not split; it becomes one part).
Common situations: Copy-pasting IPython's %set_env KEY=value syntax, forgetting the value, or quoting issues causing shlex-less split() to merge tokens.
Related errors
- Usage: %run <path>
- UsageError: Line magic function '%{name}' not found.
- UsageError: Cell magic function '%%{name}' not found.
- using '-' to denote standard input does not work in file sys
- Anthropic cache refresh response omitted usage
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/7a077f9ccfc2b932.
Report an issue: GitHub.