From 2c70fc6251a3bc5f3df7b956c89f9bc38ca2e2d6 2018-11-21 09:35:16 From: Min RK Date: 2018-11-21 09:35:16 Subject: [PATCH] add CWD to sys.path *after* stdlib this deviates from Python's own interactive behavior, but seems to avoid problems where folks create stdlib-names like `email.py`. They can still collide with packages, but that seems less common than generic names like `email` and `code` --- diff --git a/IPython/core/shellapp.py b/IPython/core/shellapp.py index 4b855c3..1cbe931 100644 --- a/IPython/core/shellapp.py +++ b/IPython/core/shellapp.py @@ -181,9 +181,26 @@ class InteractiveShellApp(Configurable): self.shell.init_user_ns() def init_path(self): - """Add current working directory, '', to sys.path""" - if sys.path[0] != '': - sys.path.insert(0, '') + """Add current working directory, '', to sys.path + + Unlike Python's default, we insert before the first `site-packages` + or `dist-packages` directory, + so that it is after the standard library. + + .. versionchanged:: 7.2 + Try to insert after the standard library, instead of first. + """ + if '' in sys.path: + return + for idx, path in enumerate(sys.path): + parent, last_part = os.path.split(path) + if last_part in {'site-packages', 'dist-packages'}: + break + else: + # no site-packages or dist-packages found (?!) + # back to original behavior of inserting at the front + idx = 0 + sys.path.insert(idx, '') def init_shell(self): raise NotImplementedError("Override in subclasses")