From 89e72e56416d8356bacd941f8ae444f116c15917 2017-12-20 12:52:24 From: Thomas Kluyver Date: 2017-12-20 12:52:24 Subject: [PATCH] Add check for exe-in-virtualenv using os.path.samefile() Closes gh-10955 --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 333d1c0..115e931 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -732,16 +732,23 @@ class InteractiveShell(SingletonConfigurable): # Not in a virtualenv return - # venv detection: + p = os.path.normcase(sys.executable) + p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) + + # executable path should end like /bin/python or \\scripts\\python.exe + p_exe_up2 = os.path.dirname(os.path.dirname(p)) + if p_exe_up2 and os.path.samefile(p_exe_up2, p_venv): + # Our exe is inside the virtualenv, don't need to do anything. + return + + # fallback venv detection: # stdlib venv may symlink sys.executable, so we can't use realpath. # but others can symlink *to* the venv Python, so we can't just use sys.executable. # So we just check every item in the symlink tree (generally <= 3) - p = os.path.normcase(sys.executable) paths = [p] while os.path.islink(p): p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) paths.append(p) - p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible if p_venv.startswith('\\cygdrive'):