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