Add a virtualenv to sys.path so the user can import modules from it. This isn't perfect: it doesn't use the Python interpreter with which the virtualenv was built, and it ignores the --no-site-packages option. A warning will appear suggesting the user installs IPython in the
(self)
| 890 | self.display_trap = DisplayTrap(hook=self.displayhook) |
| 891 | |
| 892 | def init_virtualenv(self): |
| 893 | """Add a virtualenv to sys.path so the user can import modules from it. |
| 894 | This isn't perfect: it doesn't use the Python interpreter with which the |
| 895 | virtualenv was built, and it ignores the --no-site-packages option. A |
| 896 | warning will appear suggesting the user installs IPython in the |
| 897 | virtualenv, but for many cases, it probably works well enough. |
| 898 | |
| 899 | Adapted from code snippets online. |
| 900 | |
| 901 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
| 902 | """ |
| 903 | if 'VIRTUAL_ENV' not in os.environ: |
| 904 | # Not in a virtualenv |
| 905 | return |
| 906 | |
| 907 | p = os.path.normcase(sys.executable) |
| 908 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) |
| 909 | |
| 910 | # executable path should end like /bin/python or \\scripts\\python.exe |
| 911 | p_exe_up2 = os.path.dirname(os.path.dirname(p)) |
| 912 | if p_exe_up2 and os.path.exists(p_venv) and os.path.samefile(p_exe_up2, p_venv): |
| 913 | # Our exe is inside the virtualenv, don't need to do anything. |
| 914 | return |
| 915 | |
| 916 | # fallback venv detection: |
| 917 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
| 918 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
| 919 | # So we just check every item in the symlink tree (generally <= 3) |
| 920 | paths = [p] |
| 921 | while os.path.islink(p): |
| 922 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) |
| 923 | paths.append(p) |
| 924 | |
| 925 | # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible |
| 926 | if p_venv.startswith('\\cygdrive'): |
| 927 | p_venv = p_venv[11:] |
| 928 | elif len(p_venv) >= 2 and p_venv[1] == ':': |
| 929 | p_venv = p_venv[2:] |
| 930 | |
| 931 | if any(p_venv in p for p in paths): |
| 932 | # Running properly in the virtualenv, don't need to do anything |
| 933 | return |
| 934 | |
| 935 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
| 936 | "install IPython inside the virtualenv.") |
| 937 | if sys.platform == "win32": |
| 938 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
| 939 | else: |
| 940 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
| 941 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
| 942 | |
| 943 | import site |
| 944 | sys.path.insert(0, virtual_env) |
| 945 | site.addsitedir(virtual_env) |
| 946 | |
| 947 | #------------------------------------------------------------------------- |
| 948 | # Things related to injections into the sys module |