repl-friendly-import-patching

Add project root directories to sys.path for Python imports in REPL environments.

2|Updated Jan 30, 2026
One-click install
npx skills add https://github.com/tankygranny05/agent-box --skill repl-friendly-import-patching
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: repl-friendly-import-patching
Source: https://github.com/tankygranny05/agent-box/tree/main/agent-box/seed/codex-skills/repl-friendly-import-patching
Command: npx skills add https://github.com/tankygranny05/agent-box --skill repl-friendly-import-patching

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill ensures that Python scripts can correctly import modules from parent or grandparent directories, even when run in interactive environments like PyCharm, IPython, or Jupyter notebooks where the __file__ variable might not be defined.

Core Features & Use Cases

  • Dynamic sys.path modification: Automatically adds project root directories to Python's import path.
  • REPL compatibility: Gracefully handles environments where __file__ is undefined, preventing import errors.
  • Use Case: You're developing a Python project with a structure like project_root/src/utils.py and project_root/scripts/my_script.py. This Skill allows my_script.py to import utils without manual path manipulation, whether run directly or in a Jupyter notebook.

Quick Start

Apply this Python code snippet at the beginning of your script to ensure imports from parent directories work correctly in any environment.

Frequently Asked Questions about repl-friendly-import-patching

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix Python imports in a Jupyter notebook when __file__ is not defined?

To fix Python imports when __file__ is undefined, use a try/except block to dynamically add project root directories to sys.path. This approach gracefully handles interactive environments where the variable is missing, preventing import errors.

Why does importing from a parent directory fail in IPython or PyCharm REPL?

Importing from a parent directory fails in IPython or PyCharm because the __file__ variable is often undefined in these REPL environments. Scripts attempting relative path resolution based on __file__ will raise errors without a fallback mechanism.

How do I add a project root to sys.path for Python scripting?

Add a project root to sys.path by appending the parent or grandparent directory path dynamically. Using a try/except block ensures this path modification works safely in both standard Python scripts and interactive REPL environments.

Can I use sys.path manipulation to import utils in scripts run interactively?

Yes, you can use sys.path manipulation to import utils in scripts run interactively. By combining sys.path modifications with try/except logic, your script can locate parent directories even when __file__ is not available in the REPL.

What is the best way to handle Python path errors in interactive environments?

The best way to handle Python path errors in interactive environments is implementing a try/except block around your sys.path modifications. This allows your script to fallback gracefully when __file__ is undefined in REPLs like Jupyter or IPython.

Does dynamic sys.path modification work for scripts in both terminal and Jupyter?

Yes, dynamic sys.path modification works for scripts in both terminal and Jupyter. By using try/except to handle the absence of __file__ in REPLs, the same script can successfully import from parent directories across different environments.