Files
vaiola/pythonapp/Env/Venv.py
Bacruru Sakaguchi 9e5e214944 initial commit
2025-09-12 17:10:13 +07:00

46 lines
1.4 KiB
Python

import subprocess
import os
from warnings import deprecated
try:
from .Env import Env
except ImportError:
from Env import Env
class Venv(Env):
def __init__(self, path: str, python='python3'):
super().__init__()
self.path = path
self.pip_path = os.path.join(self.path, "bin", "pip")
self.python = python
self._check_pip()
def _check_pip(self):
if not os.path.exists(self.pip_path): self.pip_path = os.path.join(self.path, "Scripts", "pip")
def create(self):
command = [self.python, '-m', 'venv', self.path]
subprocess.run(command)
def install_pkgs(self, pkgs: list, repo: str = None, extra :str = None):
self._check_pip()
command: list = [self.pip_path, "install"]
command.extend(pkgs)
if repo: command.extend(["--index-url", repo])
if extra: command.extend(["--extra-index-url", extra])
subprocess.run(command, check=True)
#@deprecated
def install_req(self, req_file: str, extra_index_url=None):
self._check_pip()
command = [self.pip_path, "install", "-r", req_file]
if extra_index_url: command.extend(["--extra-index-url", extra_index_url])
subprocess.run(command)
if __name__ == '__main__':
env = Venv(os.path.join('/tmp', 'build/test_venv'))
env.create()
env.install_pkgs(['requests', 'numpy'])