{"id":9260,"library":"python-path","title":"Python Path Context Manager","description":"The `python-path` library (version 0.1.3, last updated in 2018) provides a simple context manager for temporarily modifying `sys.path`. It offers a clean way to add directories to Python's import search path, allowing scripts to import modules located in other folders without permanent global modifications. This is particularly useful for safely loading local scripts or modules from dynamically determined paths. The project's release cadence appears inactive.","status":"maintenance","version":"0.1.3","language":"en","source_language":"en","source_url":"https://github.com/cgarciae/python_path","tags":["path","sys.path","context manager","import","module loading"],"install":[{"cmd":"pip install python-path","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"PythonPath","correct":"from python_path import PythonPath"}],"quickstart":{"code":"import os\nfrom python_path import PythonPath\n\n# Create a dummy module in a temporary directory\ntemp_dir = \"./temp_modules\"\nos.makedirs(temp_dir, exist_ok=True)\nwith open(os.path.join(temp_dir, \"my_module.py\"), \"w\") as f:\n    f.write(\"def greet():\\n    return \\\"Hello from my_module!\\\"\")\n\n# Attempt to import without modifying sys.path (will fail)\ntry:\n    import my_module\nexcept ImportError as e:\n    print(f\"Expected error: {e}\")\n\n# Use PythonPath context manager to add the directory to sys.path\nwith PythonPath(temp_dir):\n    import my_module\n    print(my_module.greet())\n\n# After exiting the 'with' block, my_module is no longer importable (sys.path restored)\ntry:\n    import my_module\nexcept ImportError as e:\n    print(f\"Expected error after context exit: {e}\")\n\n# Clean up the dummy module and directory\nos.remove(os.path.join(temp_dir, \"my_module.py\"))\nos.rmdir(temp_dir)","lang":"python","description":"This quickstart demonstrates how to use the `PythonPath` context manager to temporarily add a directory to `sys.path` for importing modules, and how `sys.path` is automatically restored upon exiting the `with` block."},"warnings":[{"fix":"For general path manipulation, use Python's built-in `pathlib` module. For more robust `sys.path` management in complex projects, evaluate tools like `importlib` utilities or project-specific solutions for module discovery, or consider if the project structure itself can be simplified to avoid `sys.path` manipulation.","message":"The `python-path` library (cgarciae/python_path) has not been updated since 2018. Consider if `pathlib` (standard library) or more actively maintained alternatives for managing import paths are more suitable for new projects.","severity":"deprecated","affected_versions":"<=0.1.3"},{"fix":"Ensure unique module names across your project or be explicit with import statements. If name collisions are unavoidable, carefully manage the order in which paths are added (implicitly, `PythonPath` adds to the beginning of `sys.path` for the duration of the context).","message":"While `python-path` temporarily modifies `sys.path`, it doesn't prevent name collisions if modules with the same name exist in different added paths or standard library locations. The first module found in `sys.path` will be imported.","severity":"gotcha","affected_versions":"<=0.1.3"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Wrap your import statement within a `with PythonPath('path/to/module_directory'):` block to temporarily add the directory to `sys.path`.","cause":"The Python interpreter cannot find the specified module because its containing directory is not in `sys.path`.","error":"ModuleNotFoundError: No module named 'my_module'"},{"fix":"Ensure the library is installed with `pip install python-path` and use `from python_path import PythonPath`.","cause":"This usually indicates that the `python-path` library is not installed or the import statement is incorrect.","error":"ImportError: cannot import name 'PythonPath' from 'python_path' (unknown location)"}]}