pyenv / pyenv-virtualenv

a pyenv plugin to manage virtualenv (a.k.a. python-virtualenv)
MIT License
6.37k stars 403 forks source link

cannot change environment in subprocess #414

Open mbway opened 2 years ago

mbway commented 2 years ago

Activating a virtual environment from inside a python script running in another virtual environment fails. For example I have a 'script runner' application which has a set of scripts and their associated environments.

Here is a minimal example of the problem:

import subprocess
subprocess.run(['bash', '-c', 'source activate myenv && python -c "import sys;print(sys.executable)"'])

output:

$ /usr/bin/python runner.py  # running using an interpreter not managed by pyenv
pyenv-virtualenv: activate myenv
/home/matthew/.pyenv/versions/myenv/bin/python

$ python runner.py  # running from a pyenv environment
/home/matthew/.pyenv/versions/3.9.9/envs/otherenv/bin/python  # environment did not switch

Details