I want to create a temp file and write a python script. I want to create a virtual environment at this temp dir and use pip and then run this script with the virtual environment.
import pathlib
import tempfile
import venv
temp = tempfile.TemporaryDirectory()
virtualenv = venv.EnvBuilder(system_site_packages=false)
virtualenv.create(temp.name)
# how could I activate the virtual environment and install the pip module?
with open(pathlib.Path(temp.name)/"run.py", "w") as f:
f.write("#/usr/bin/env python\n\n")
f.write("import requests\n")
p = subprocess.run(["python", temp.name", "run.py"])