On Windows (11), I'm trying to activate a python virtualenv inside a subprocess.run
, but, I'm not able to execute the activate script :
import subprocess
p = subprocess.run(".\venv\Scripts\activate.bat", shell=True, stderr=subprocess.PIPE)
print(p.stderr)
Produce :
b"'.' n'est pas reconnu en tant que commande interne\r\nou externe, un programme ex\x82cutable ou un fichier de commandes.\r\n"
Note: .\venv\Scripts\activate.bat
is working in a shell prompt.
How am I supposed to activate a virtual env inside a subprocess.run
?
CodePudding user response:
Remember that \
is used to escape special characters. \v
is a ASCII Vertical Tab, so that the first part of your command line is just .
. To fix this, add an r
to your string.
p = subprocess.run([r".\venv\Scripts\activate.bat"], stderr=subprocess.PIPE)
Note that I've also removed the shell=True. It's not necessary and so shouldn't be used.
Oh, and remember that you cannot change one process' environment from a subprocess. So, whatever you're trying to do, probably won't work anyways.