Created
March 22, 2020 11:04
-
-
Save Feuermurmel/0a3ef5d497106a9067838f9a6460243e to your computer and use it in GitHub Desktop.
Relative paths in Python stack traces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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