pyvenv
and the venv
module don't support this out of the box. The third party virtualenv
package does support this using activate_this.py
, but that feature was not included in the built-in venv
module.
You could try to borrow a copy of activate_this.py
from a virtualenv
based environment; it seems to work, though I can't swear it will be perfect (venv
/pyvenv
uses some magic during startup; unclear if all of it is replicated via activate_this.py
).
The virtualenv
docs for it are out of date for Python 3 (they claim you use execfile
, which doesn't exist). The Python 3 compatible alternative would be:
activator = 'some/path/to/activate_this.py' # Looted from virtualenv; should not require modification, since it's defined relatively
with open(activator) as f:
exec(f.read(), {'__file__': activator})
Nothing activate_this.py
does is magical, so you could manually perform the same changes without looting from virtualenv
(adjusting PATH
, sys.path
, sys.prefix
, etc.), but borrowing makes it much simpler in this case.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…