##// END OF EJS Templates
Merge pull request #1388 from takluyver/venv-support...
Fernando Perez -
r6105:0a50e77a merge
parent child Browse files
Show More
@@ -412,6 +412,9 b' class InteractiveShell(SingletonConfigurable, Magic):'
412 self.init_instance_attrs()
412 self.init_instance_attrs()
413 self.init_environment()
413 self.init_environment()
414
414
415 # Check if we're in a virtualenv, and set up sys.path.
416 self.init_virtualenv()
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)
417 # This has to be done after init_create_namespaces because it uses
420 # This has to be done after init_create_namespaces because it uses
@@ -663,6 +666,37 b' class InteractiveShell(SingletonConfigurable, Magic):'
663 except ImportError:
666 except ImportError:
664 warn("doctest module does not exist.")
667 warn("doctest module does not exist.")
665
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)
699
666 #-------------------------------------------------------------------------
700 #-------------------------------------------------------------------------
667 # Things related to injections into the sys module
701 # Things related to injections into the sys module
668 #-------------------------------------------------------------------------
702 #-------------------------------------------------------------------------
General Comments 0
You need to be logged in to leave comments. Login now