Skip to content

Instantly share code, notes, and snippets.

@Feuermurmel
Created March 22, 2020 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Feuermurmel/0a3ef5d497106a9067838f9a6460243e to your computer and use it in GitHub Desktop.
Save Feuermurmel/0a3ef5d497106a9067838f9a6460243e to your computer and use it in GitHub Desktop.
Relative paths in Python stack traces.
import builtins
import os
import sys
from builtins import __import__
def fix_path(p: str):
if not p.startswith('/'):
return p
rel_p = os.path.relpath(p)
if rel_p.startswith('..'):
return p
return rel_p
old_length = 0
def new_import(*args):
global old_length
if len(sys.path) != old_length:
sys.path[:] = map(fix_path, sys.path)
old_length = len(sys.path)
return __import__(*args)
builtins.__import__ = new_import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment