##// END OF EJS Templates
Merge pull request #13146 from meeseeksmachine/auto-backport-of-pr-12548-on-7.x...
Blazej Michalik -
r26784:3434b6c9 merge
parent child Browse files
Show More
@@ -28,6 +28,7 b' import subprocess'
28 28 import warnings
29 29 from io import open as io_open
30 30
31 from pathlib import Path
31 32 from pickleshare import PickleShareDB
32 33
33 34 from traitlets.config.configurable import SingletonConfigurable
@@ -892,7 +893,7 b' class InteractiveShell(SingletonConfigurable):'
892 893 self.display_trap = DisplayTrap(hook=self.displayhook)
893 894
894 895 def init_virtualenv(self):
895 """Add a virtualenv to sys.path so the user can import modules from it.
896 """Add the current virtualenv to sys.path so the user can import modules from it.
896 897 This isn't perfect: it doesn't use the Python interpreter with which the
897 898 virtualenv was built, and it ignores the --no-site-packages option. A
898 899 warning will appear suggesting the user installs IPython in the
@@ -905,42 +906,42 b' class InteractiveShell(SingletonConfigurable):'
905 906 if 'VIRTUAL_ENV' not in os.environ:
906 907 # Not in a virtualenv
907 908 return
908
909 p = os.path.normcase(sys.executable)
910 p_venv = os.path.normcase(os.environ['VIRTUAL_ENV'])
911
912 # executable path should end like /bin/python or \\scripts\\python.exe
913 p_exe_up2 = os.path.dirname(os.path.dirname(p))
914 if p_exe_up2 and os.path.exists(p_venv) and os.path.samefile(p_exe_up2, p_venv):
915 # Our exe is inside the virtualenv, don't need to do anything.
909 elif os.environ["VIRTUAL_ENV"] == "":
910 warn("Virtual env path set to '', please check if this is intended.")
916 911 return
917 912
913 p = Path(sys.executable)
914 p_venv = Path(os.environ["VIRTUAL_ENV"])
915
918 916 # fallback venv detection:
919 917 # stdlib venv may symlink sys.executable, so we can't use realpath.
920 918 # but others can symlink *to* the venv Python, so we can't just use sys.executable.
921 919 # So we just check every item in the symlink tree (generally <= 3)
922 920 paths = [p]
923 while os.path.islink(p):
924 p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p)))
925 paths.append(p)
926
921 while p.is_symlink():
922 p = Path(os.readlink(p))
923 paths.append(p.resolve())
924
927 925 # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
928 if p_venv.startswith('\\cygdrive'):
929 p_venv = p_venv[11:]
930 elif len(p_venv) >= 2 and p_venv[1] == ':':
931 p_venv = p_venv[2:]
926 if str(p_venv).startswith("\\cygdrive"):
927 p_venv = Path(str(p_venv)[11:])
928 elif len(str(p_venv)) >= 2 and str(p_venv)[1] == ":":
929 p_venv = Path(str(p_venv)[2:])
932 930
933 if any(p_venv in p for p in paths):
934 # Running properly in the virtualenv, don't need to do anything
931 if any(os.fspath(p_venv) in os.fspath(p) for p in paths):
932 # Our exe is inside or has access to the virtualenv, don't need to do anything.
935 933 return
936
934
937 935 warn("Attempting to work in a virtualenv. If you encounter problems, please "
938 936 "install IPython inside the virtualenv.")
939 937 if sys.platform == "win32":
940 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
938 virtual_env = Path(os.environ["VIRTUAL_ENV"]).joinpath(
939 "Lib", "site-packages"
940 )
941 941 else:
942 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
943 'python%d.%d' % sys.version_info[:2], 'site-packages')
942 virtual_env = Path(os.environ["VIRTUAL_ENV"]).joinpath(
943 "lib", "python{}.{}".format(*sys.version_info[:2]), "site-packages"
944 )
944 945
945 946 import site
946 947 sys.path.insert(0, virtual_env)
General Comments 0
You need to be logged in to leave comments. Login now