python-execution-fallback
ما الذي تفعله
تمنح هذه المهارة وكيل الذكاء الاصطناعي ترتيبًا واضحًا للخطوات عندما تفشل شيفرة Python في التشغيل. وهي مناسبة لمن يحتاج إلى طريقة موثوقة لتشغيل شيفرة Python عبر الذكاء الاصطناعي.
متى تستخدمها
- ✓عندما تفشل شيفرة Python المكتوبة مباشرة بخطأ في الصياغة أو وقت التشغيل.
- ✓عندما تعطي شيفرة heredoc مشكلات في التحليل وأحتاج إلى نهج أكثر استقرارًا.
- ✓عندما يفشل إنشاء جدول بيانات أو معالجة بيانات عند تشغيله مباشرة.
رابط المهارة
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-execution-fallback/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“شيفرة Python الخاصة بي تفشل باستمرار عند تشغيلها مباشرة. من فضلك احفظها كسكربت وشغّله، ثم تحقق من النتائج.”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
يجرب الوكيل الشيفرة مباشرة أولًا. فإذا فشلت يحفظ السكربت كاملًا في ملف .py، ويشغّله عبر الطرفية، ويتحقق من أن الناتج كامل وصحيح.
المحتوى التقني (ملف SKILL.md الأصلي)
النص الأصلي من المؤلف، مباشرة من GitHub.
Python Execution Fallback Workflow
This skill provides a systematic debugging approach when inline Python code execution fails, particularly in heredoc or shell_agent contexts.
Pattern Overview
When Python code execution fails, follow this recovery workflow:
- Attempt inline execution first - Try running Python code directly
- Write script to file on failure - Persist the code to a
.pyfile - Execute the file - Run the saved script via shell
- Validate output - Verify results are correct and complete
When to Use
Apply this pattern when:
- Inline Python execution fails with syntax or runtime errors
- Heredoc-based code execution encounters parsing issues
- Working with complex multi-line scripts that need debugging
- Need systematic approach to isolate execution failures
- Spreadsheet/data generation tasks fail in inline mode
Step-by-Step Instructions
Step 1: Attempt Inline Execution
Try executing Python code inline first (using execute_code_sandbox or similar):
# Example: Attempt inline execution
import pandas as pd
import openpyxl
df = pd.read_excel("input.xlsx")
# Process data...
df.to_excel("output.xlsx", index=False)
If this succeeds, proceed. If it fails, move to Step 2.
Step 2: Write Script to File (On Failure)
When inline execution fails, write the complete script to a persistent file:
# Capture the script content
script_content = '''
import pandas as pd
import openpyxl
import sys
try:
# Your original code here
df = pd.read_excel("input.xlsx")
# Processing logic
result_df = df.groupby("category").sum()
# Output
result_df.to_excel("output.xlsx", index=False)
print("SUCCESS: File generated")
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
sys.exit(1)
'''
# Write to file
with open("script.py", "w") as f:
f.write(script_content)
print("Script written to script.py")
Step 3: Execute the File
Run the saved script using shell execution:
python script.py
Or with error capture:
python script.py 2>&1 | tee execution.log
This approach:
- Avoids heredoc parsing issues
- Provides clearer error messages
- Allows script inspection and modification
- Enables re-execution without rewriting
Step 4: Validate Output
Verify the results are correct:
# Validation script
import pandas as pd
import os
# Check file exists
if os.path.exists("output.xlsx"):
df = pd.read_excel("output.xlsx")
print(f"Rows: {len(df)}, Columns: {len(df.columns)}")
print(df.head())
print("VALIDATION: PASSED")
else:
print("VALIDATION: FAILED - Output file missing")
Code Example: Complete Workflow
# Full fallback workflow example
def generate_spreadsheet_fallback(data, output_path):
"""Generate spreadsheet with fallback workflow"""
# Step 1: Try inline
inline_script = f'''
import pandas as pd
data = {data}
df = pd.DataFrame(data)
df.to_excel("{output_path}", index=False)
'''
try:
# Attempt inline execution
result = execute_code_sandbox(code=inline_script)
if "error" not in result.lower():
return "SUCCESS_INLINE"
except Exception as e:
pass
# Step 2: Write to file
file_script = f'''
import pandas as pd
import sys
try:
data = {data}
df = pd.DataFrame(data)
df.to_excel("{output_path}", index=False)
print("SUCCESS")
except Exception as e:
print(f"ERROR: {{e}}", file=sys.stderr)
sys.exit(1)
'''
with open("generate.py", "w") as f:
f.write(file_script)
# Step 3: Execute file
shell_result = run_shell(command="python generate.py")
# Step 4: Validate
if os.path.exists(output_path):
return "SUCCESS_FILE"
else:
return "FAILED"
Best Practices
- Include error handling in file-based scripts (try/except with sys.stderr)
- Add success indicators (print statements) to confirm execution
- Preserve original logic when converting from inline to file
- Check return codes when executing scripts via shell
- Clean up temporary files after successful validation
- Log both stdout and stderr for debugging
Common Failure Scenarios
This pattern helps resolve:
- Heredoc string escaping issues
- Multi-line code formatting problems
- Import path resolution failures
- Environment variable access issues
- Complex indentation in inline code
الأسئلة الشائعة
لماذا نحفظ الشيفرة في ملف؟
الملف المحفوظ أسهل في الإصلاح وإعادة التشغيل من الشيفرة المكتوبة مباشرة، ويبقى للمراجعة.
ماذا تتحقق خطوة التحقق؟
تتحقق من أن الناتج صحيح وكامل، مثل وجود الصفوف أو الأوراق المتوقعة.
هل يبدأ الوكيل دائمًا بالتشغيل المباشر؟
نعم، تقول المهارة أن يجرب الشيفرة مباشرة أولًا، ولا يتحول إلى ملف إلا عند الفشل.
التفاصيل
الوصف الأصلي من المؤلف
Four-step recovery workflow for code execution failures when inline Python fails
repo: HKUDS/AgentPilot
الشعبية
- نجوم GitHub
- 7.8k
- الترتيب
- #12959
- الترخيص
- MIT
- آخر تحديث
- 1/11/2026
التعليقات
- …
المزيد من المهارات من هذا المستودع
agentpilot
7.8kيساعد وكيلك على تحسين مشروع خطوة بخطوة، بتجهيز مهمة وتشغيل حلقة AgentPilot التي تقترح التغييرات وتختبرها وتقيّمها.
openspace
7.8kيساعد وكيلك على إعداد OpenSpace، وهو أداة تخزن مهارات الوكلاء وتسترجعها، وربطه بتطبيق الذكاء الاصطناعي عبر MCP.
rsi-arena
7.8kيساعد وكيلك على إعداد اختبارات معيارية مثل AIME وTerminal-Bench وتشغيلها، لمقارنة طرق التحسين المختلفة.
experiment-guide
7.8kيساعد وكيلك على تخطيط تجارب صغيرة وقراءة نتائجها وتسجيل ما يتعلمه أثناء التحقيق في طرق تحسين برنامج.
مهارات مشابهة
diffs
394kيتيح لوكيلك إنتاج فرق تعديلات حقيقي يمكن مشاركته، كرابط عرض أو ملف صورة، بدلاً من ملخص مكتوب يدوياً.
coding-agent
394kيساعد وكيلك على تسليم مهمة برمجة كبيرة إلى Codex أو Claude Code أو OpenCode كعامل في الخلفية، ثم متابعة تقدمها.
gh-issues
394kيساعد وكيلك على إيجاد مشكلات GitHub واختيار ما ستعمل عليه، وفتح طلبات سحب بالإصلاحات، ومتابعة تعليقات المراجعة.