Running a subprocess in a venv

Mats Wichmann mats at wichmann.us
Sat Oct 21 15:08:03 EDT 2023


On 10/21/23 07:01, Larry Martell via Python-list wrote:
> I have a python script, and from that I want to run another script in
> a subprocess in a venv. What is the best way to do that? I could write
> a file that activates the venv then runs the script, then run that
> file, but that seems messy. Is there a better way?

You don't need to "activate" a virtualenv. The activation script does 
some helpful things along the way (setup and cleanup) but none of them 
are required.  The most important thing it does is basically:

VIRTUAL_ENV='path-where-you-put-the-virtualenv'
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

and that's really only so that commands that belong to that virtualenv 
(python, pip, and things where you installed a package in the venv wich 
creates an "executable" in bin/) are in a directory first in your search 
path. As long as you deal with necessary paths yourself, you're fine 
without activating. So as mentioned elsewhere, just use the path to the 
virtualenv's Python and you're good to go.




More information about the Python-list mailing list