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 | |
@@ -892,7 +893,7 b' class InteractiveShell(SingletonConfigurable):' | |||||
892 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
893 | self.display_trap = DisplayTrap(hook=self.displayhook) | |
893 |
|
894 | |||
894 | def init_virtualenv(self): |
|
895 | def init_virtualenv(self): | |
895 |
"""Add |
|
896 | """Add the current virtualenv to sys.path so the user can import modules from it. | |
896 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
897 | This isn't perfect: it doesn't use the Python interpreter with which the | |
897 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
898 | virtualenv was built, and it ignores the --no-site-packages option. A | |
898 | warning will appear suggesting the user installs IPython in the |
|
899 | warning will appear suggesting the user installs IPython in the | |
@@ -905,42 +906,42 b' class InteractiveShell(SingletonConfigurable):' | |||||
905 | if 'VIRTUAL_ENV' not in os.environ: |
|
906 | if 'VIRTUAL_ENV' not in os.environ: | |
906 | # Not in a virtualenv |
|
907 | # Not in a virtualenv | |
907 | return |
|
908 | return | |
908 |
|
909 | elif os.environ["VIRTUAL_ENV"] == "": | ||
909 | p = os.path.normcase(sys.executable) |
|
910 | warn("Virtual env path set to '', please check if this is intended.") | |
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. |
|
|||
916 | return |
|
911 | return | |
917 |
|
912 | |||
|
913 | p = Path(sys.executable) | |||
|
914 | p_venv = Path(os.environ["VIRTUAL_ENV"]) | |||
|
915 | ||||
918 | # fallback venv detection: |
|
916 | # fallback venv detection: | |
919 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
917 | # stdlib venv may symlink sys.executable, so we can't use realpath. | |
920 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
918 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. | |
921 | # So we just check every item in the symlink tree (generally <= 3) |
|
919 | # So we just check every item in the symlink tree (generally <= 3) | |
922 | paths = [p] |
|
920 | paths = [p] | |
923 |
while |
|
921 | while p.is_symlink(): | |
924 |
p = |
|
922 | p = Path(os.readlink(p)) | |
925 | paths.append(p) |
|
923 | paths.append(p.resolve()) | |
926 |
|
924 | |||
927 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible |
|
925 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible | |
928 |
if p_venv.startswith( |
|
926 | if str(p_venv).startswith("\\cygdrive"): | |
929 | p_venv = p_venv[11:] |
|
927 | p_venv = Path(str(p_venv)[11:]) | |
930 |
elif len(p_venv) >= 2 and p_venv[1] == |
|
928 | elif len(str(p_venv)) >= 2 and str(p_venv)[1] == ":": | |
931 | p_venv = p_venv[2:] |
|
929 | p_venv = Path(str(p_venv)[2:]) | |
932 |
|
930 | |||
933 | if any(p_venv in p for p in paths): |
|
931 | if any(os.fspath(p_venv) in os.fspath(p) for p in paths): | |
934 |
# |
|
932 | # Our exe is inside or has access to the virtualenv, don't need to do anything. | |
935 | return |
|
933 | return | |
936 |
|
934 | |||
937 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
935 | warn("Attempting to work in a virtualenv. If you encounter problems, please " | |
938 | "install IPython inside the virtualenv.") |
|
936 | "install IPython inside the virtualenv.") | |
939 | if sys.platform == "win32": |
|
937 | if sys.platform == "win32": | |
940 |
virtual_env = |
|
938 | virtual_env = Path(os.environ["VIRTUAL_ENV"]).joinpath( | |
|
939 | "Lib", "site-packages" | |||
|
940 | ) | |||
941 | else: |
|
941 | else: | |
942 |
virtual_env = |
|
942 | virtual_env = Path(os.environ["VIRTUAL_ENV"]).joinpath( | |
943 |
|
|
943 | "lib", "python{}.{}".format(*sys.version_info[:2]), "site-packages" | |
|
944 | ) | |||
944 |
|
945 | |||
945 | import site |
|
946 | import site | |
946 | 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