humanerd-drew/opencode-drewgent/python-nested-import-nameerror
Claude Code and agent skill by humanerd-drew/opencode-drewgent
- community
Checking that the SKILL.md file is still in the repository.
Python nested function에서 __import__('json') 사용 시 UnboundLocalError 발생 원인 + 방지 규칙
Install
npx vskill@latest install humanerd-drew/opencode-drewgent/python-nested-import-nameerrorSKILL.md
The same SKILL.md was published earlier as humanerd-drew/opencode-drewgent/python-nested-import-nameerror.
Python Nested Import NameError Bug Pattern
Python 함수 내부에서 import json 또는 import json as X를 실행하면, 같은 함수 내 모든 json 참조가 local variable가 됨. 선언보다 앞에서 json.dumps() 등을 호출하면 → UnboundLocalError.
Root Cause — Python Lexical Scoping
Python 컴파일러가 함수 전체를 훑는다. 함수 어딘가에서 import json as X를 보면, 해당 함수 스코프의 json 이름을 local variable로 등록한다. 이후 json.dumps()가 호출되더라도, local json은 아직 값이 할당되지 않았으므로 → UnboundLocalError.
The Fix — module-level import 우선
Applied Fixes in run_agent.py
2026-05-30 fix — run_conversation() 내부 4곳 수정:
| 줄 | 수정 전 | 수정 후 | 이유 | |
|---|---|---|---|---|
| 10929 | import json as _json_mod | # json is module-level (line 29) 주석 처리 | nested import 제거 — json 이름 local로 shadowing됨 | |
| 10934 | _json_mod.dumps(args) | json.dumps(args) | module-level json 직접 사용 | |
| 10944 | _json_mod.loads(args) | json.loads(args) | module-level json 직접 사용 | |
| 11548 | import os, json | # os, json are module-level 주석 처리 | nested import 제거 |
주의: 이전 수정(2026-05-29)에서 import json as _json_mod를 주석 처리하고 json.dumps() 사용으로 바꿨지만, 같은 함수 스코프 내의 json.loads() 호출은 여전히 _json_mod.loads()로 남아있음 → NameError: name '_json_mod' is not defined 발생
정답: 모든 nested import json 제거 → module-level import json (line 29) 하나만 사용
Detection Commands
Discovery Timeline
- 2026-05-29 03:14 KST: SEO + Trend cron jobs both failed with
Error during OpenAI-compatible API call #89: cannot access local variable 'json' where it is not associated with a value - Root cause:
run_agent.pyline 10929:import json as _json_modinsiderun_conversation()→jsonbecomes local variable in function scope - Trigger: API call #89에서
json.loads(args)실행 시도시 → localjson은 아직 값 없음 →UnboundLocalError - 2026-05-29 fix: line 10929 주석 처리,
json.dumps()사용으로 변경 — 하지만_json_mod.loads()미수정 - 2026-05-30 14:05 KST: manual trigger에서
_json_mod is not definedNameError 여전히 발생 — log에서 확인 - 2026-05-30 14:07 KST: line 10944
_json_mod.loads(args)→json.loads(args)수정 — 2차 fix - 2026-05-30 14:09 KST: SEO Article Harvester 성공 (35개 기사 수집, heritage labeling 완료)
- 2026-05-30 14:15 KST: Trend Harvester 성공 (175 트렌드 수집, memory sync 완료)
- Gateway log:
CRITICAL json NameError0건確認 — 완전히 제거됨
Prevention Rule
禁karpathy_coding_principles 적용 — "Think Before Coding":
IF YOU DON'T KNOW how Python scoping works in a nested function, ASK. Don't guess.
함수 내부에서 import json을 쓸 때는:
- module-level
import json이 이미 있는지 확인 (run_agent.py line 29) - 이미 있으면 함수 내부 import 불필요 → module-level 직접 사용
- 불가피 시
import json as _json_mod후_json_mod.dumps()— but NEVERimport jsonwithout alias - 수정 후 반드시
grep -n "_json_mod" run_agent.py로 잔여 확인
Generated: 2026-05-30 — root cause verified after 2nd fix
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/software-development/python-nested-import-nameerror/SKILL.md
Popularity
- Repository stars
- 3
- Repository forks
- 8
- 7-day trend score
- 0
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/humanerd-drew/opencode-drewgent/python-nested-import-nameerror)