##// END OF EJS Templates
Merge pull request #1388 from takluyver/venv-support...
Fernando Perez -
r6105:0a50e77a merge
parent child Browse files
Show More
@@ -411,6 +411,9 b' class InteractiveShell(SingletonConfigurable, Magic):'
411 self.init_profile_dir(profile_dir)
411 self.init_profile_dir(profile_dir)
412 self.init_instance_attrs()
412 self.init_instance_attrs()
413 self.init_environment()
413 self.init_environment()
414
415 # Check if we're in a virtualenv, and set up sys.path.
416 self.init_virtualenv()
414
417
415 # Create namespaces (user_ns, user_global_ns, etc.)
418 # Create namespaces (user_ns, user_global_ns, etc.)
416 self.init_create_namespaces(user_module, user_ns)
419 self.init_create_namespaces(user_module, user_ns)
@@ -662,6 +665,37 b' class InteractiveShell(SingletonConfigurable, Magic):'
662 doctest_reload()
665 doctest_reload()
663 except ImportError:
666 except ImportError:
664 warn("doctest module does not exist.")
667 warn("doctest module does not exist.")
668
669 def init_virtualenv(self):
670 """Add a virtualenv to sys.path so the user can import modules from it.
671 This isn't perfect: it doesn't use the Python interpreter with which the
672 virtualenv was built, and it ignores the --no-site-packages option. A
673 warning will appear suggesting the user installs IPython in the
674 virtualenv, but for many cases, it probably works well enough.
675
676 Adapted from code snippets online.
677
678 http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
679 """
680 if 'VIRTUAL_ENV' not in os.environ:
681 # Not in a virtualenv
682 return
683
684 if sys.executable.startswith(os.environ['VIRTUAL_ENV']):
685 # Running properly in the virtualenv, don't need to do anything
686 return
687
688 warn("Attempting to work in a virtualenv. If you encounter problems, please "
689 "install IPython inside the virtualenv.\n")
690 if sys.platform == "win32":
691 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages')
692 else:
693 virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib',
694 'python%d.%d' % sys.version_info[:2], 'site-packages')
695
696 import site
697 sys.path.insert(0, virtual_env)
698 site.addsitedir(virtual_env)
665
699
666 #-------------------------------------------------------------------------
700 #-------------------------------------------------------------------------
667 # Things related to injections into the sys module
701 # Things related to injections into the sys module
General Comments 0
You need to be logged in to leave comments. Login now