-
Notifications
You must be signed in to change notification settings - Fork 0
/
venv_tool.py
executable file
·76 lines (64 loc) · 2.91 KB
/
venv_tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/python3
"""
Virtual environment creation and activation
"""
# References:
# * [venv — Creation of virtual environments](https://docs.python.org/3/library/venv.html)
# * [PEP 405 – Python Virtual Environments](https://peps.python.org/pep-0405/)
# * [site — Site-specific configuration hook](https://docs.python.org/3/library/site.html)
# * [Install packages in a virtual environment using pip and venv](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/)
# * [Python Virtual Environments: A Primer](https://realpython.com/python-virtual-environments-a-primer/)
# * [Activate a virtualenv with a Python script](https://stackoverflow.com/questions/6943208/activate-a-virtualenv-with-a-python-script#answer-68173529)
import os
import sys
venv_dir = os.path.join(os.path.dirname(__file__), ".venv")
def activate(dist_packages=False):
"""
If we have a Python virtual environment, activate it.
"""
if os.path.exists(os.path.join(venv_dir, "pyvenv.cfg")):
sys.prefix = sys.exec_prefix = os.path.abspath(venv_dir)
# Drop packages added by the Linux distribution from the path
if not dist_packages:
sys.path = list(filter(lambda item: not item.endswith("/dist-packages"), sys.path))
# Add packages from the virtual environment to the path
import site
if sys.platform == "win32":
site_packages = os.path.join(sys.prefix, "Lib", "site-packages")
else:
site_packages = os.path.join(sys.prefix, f"lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages")
site.addsitedir(site_packages)
#print(sys.path)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--create", action="store_true", help="Create a virtual environment")
parser.add_argument("--update", action="store_true", help="Update the virtual environment")
parser.add_argument("--delete", action="store_true", help="Delete the virtual environment")
options = parser.parse_args()
# Install or upgrade the virtual environment
if options.create:
if os.path.exists(venv_dir):
print("Venv already exists. Use --delete or --update.")
sys.exit(1)
import venv
venv.create(venv_dir, with_pip=True)
elif options.update:
import venv
venv.create(venv_dir, upgrade_deps=True)
# Install or upgrade the packages listed in requirements.txt
if options.create or options.update:
activate()
import subprocess
if sys.platform == "win32":
python = os.path.join(venv_dir, "Scripts", "python")
else:
python = os.path.join(venv_dir, "bin", "python")
if sys.platform == "win32":
subprocess.check_call([python, "-m", "pip", "install", "https://github.com/z-mahmud22/Dlib_Windows_Python3.x/raw/refs/heads/main/dlib-19.24.99-cp312-cp312-win_amd64.whl"])
subprocess.check_call([python, "-m", "pip", "install", "-r", "requirements.txt"])
elif options.delete:
import shutil
shutil.rmtree(venv_dir)
else:
parser.print_help()