This is a complement to the answer @TomDalton gave. Althought there's no an automatically provided way to do this, there are means provided by the activate_this
script.
First, it is important to remember that this line:
execfile(virtualenv, dict(__file__=virtualenv))
is calling the function without passing a dictionary for globals nor a dictionary for locals. This will imply that the execution context will be the current globals (i.e. the one from the calling line) and locals objects. such line will alter the variables we currently have -- will override the calling environment See here for docs about.
In that way, since variables are overriden, activate_this
gives us some variables like:
old_os_path = os.environ['PATH']
#the previous PATH
prev_sys_path = list(sys.path)
#the old Python Path
if sys.platform == 'win32':
site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
#site_packages is the extended venv's packages dir.
#sys.path is affected here
sys.real_prefix = sys.prefix
#the old system prefix
So we can restore such variables if we want a manual deactivation:
import sys, os, site
sys.path[:0] = prev_sys_path #will also revert the added site-packages
sys.prefix = sys.real_prefix
os.setenv('PATH', old_os_path)