I would like to write a python script that does 3 things :
- build a virtual environment with python3
- activate this new virtual env. ( bash: source myvirtenv/bin/acticate)
- install packages with a requirements.txt (bash: pip install -r )
In my project I use the normal virtualenviroment package . and I have to do i on a Debian machine.
I tried to mimic the bash command with os.system() but didn't make it with the code below.
import os os.system('python3 -m venv test6_env') os.system('source test6_env/bin/activate') os.system( 'pip install -r requirements.txt --user')
Problem the virtualenv will not activated and the requirements not installed.
Is there a easy trick to script in python this 3 stepy nicely ?
Thank you for your help RÃ©mi
The problem is that
source test6_env/bin/activate activates the virtual environment only for the subshell used by that
os.system() call, and not any subsequent ones. Instead, run all shell commands with a single call, e.g.
os.system('python3 -m venv test6_env && source test6_env/bin/activate && pip install -r requirements.txt --user')