Running a subprocess in a venv

Roel Schroeven roel at roelschroeven.net
Sat Oct 21 10:40:43 EDT 2023


Larry Martell via Python-list schreef op 21/10/2023 om 15:01:
> 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?
Activating a venv it is practical when you're working in a shell, but 
not actually needed. You can execute the python in the venv with the 
script as parameter.

Have a look in the venv directory: there will be a Script subdirectory 
(on Windows) or bin subdirectory (on Unix-like systems). Within that 
directory are several executables, one of which will be python or 
python3. That's the one you need.

So use something like

     subprocess.run(['/path/to/venv/bin/python3', 'yourscript.py', 
possible other arguments])

-- 
"Binnen een begrensde ruimte ligt een kritiek punt, waar voorbij de vrijheid
afneemt naarmate het aantal individuen stijgt. Dit gaat evenzeer op voor mensen
in de begrensde ruimte van een planetair ecosysteem, als voor de gasmoleculen
in een hermetisch gesloten vat. Bij mensen is het niet de vraag hoeveel er
maximaal in leven kunnen blijven in het systeem, maar wat voor soort bestaan
mogelijk is voor diegenen die in leven blijven.
           -- Pardot Kynes, eerste planetoloog van Arrakis"
           -- Frank Herbert, Duin



More information about the Python-list mailing list