-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathen-install_with_log.py
More file actions
37 lines (31 loc) · 1.14 KB
/
en-install_with_log.py
File metadata and controls
37 lines (31 loc) · 1.14 KB
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
import subprocess
import logging
from pathlib import Path
# Configure logging
logfile = "pip_install2.log"
logging.basicConfig(
filename=logfile,
filemode='w',
format='%(asctime)s [%(levelname)s] %(message)s',
level=logging.INFO
)
def install_requirements(requirements_path):
requirements = Path(requirements_path)
if not requirements.exists():
logging.error(f"File not found: {requirements_path}")
print(f"[Error] File not found: {requirements_path}")
return
with requirements.open("r", encoding="utf-8") as f:
packages = [line.strip() for line in f if line.strip() and not line.startswith("#")]
for pkg in packages:
print(f"Installing: {pkg}")
logging.info(f"Installing {pkg}")
try:
subprocess.check_call(["pip", "install", pkg])
logging.info(f"Success: {pkg}")
except subprocess.CalledProcessError as e:
logging.error(f"Failed: {pkg} -- {e}")
print(f"[Error] Failed to install {pkg}")
print(f"\nCompleted. Log is saved in '{logfile}'.")
if __name__ == "__main__":
install_requirements("requirements_pip.txt")