michael@0: """By using execfile(this_file, dict(__file__=this_file)) you will michael@0: activate this virtualenv environment. michael@0: michael@0: This can be used when you must use an existing Python interpreter, not michael@0: the virtualenv bin/python michael@0: """ michael@0: michael@0: try: michael@0: __file__ michael@0: except NameError: michael@0: raise AssertionError( michael@0: "You must run this like execfile('path/to/activate_this.py', dict(__file__='path/to/activate_this.py'))") michael@0: import sys michael@0: import os michael@0: michael@0: old_os_path = os.environ['PATH'] michael@0: os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path michael@0: base = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) michael@0: if sys.platform == 'win32': michael@0: site_packages = os.path.join(base, 'Lib', 'site-packages') michael@0: else: michael@0: site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages') michael@0: prev_sys_path = list(sys.path) michael@0: import site michael@0: site.addsitedir(site_packages) michael@0: sys.real_prefix = sys.prefix michael@0: sys.prefix = base michael@0: # Move the added items to the front of the path: michael@0: new_sys_path = [] michael@0: for item in list(sys.path): michael@0: if item not in prev_sys_path: michael@0: new_sys_path.append(item) michael@0: sys.path.remove(item) michael@0: sys.path[:0] = new_sys_path