-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (43 loc) · 1.93 KB
/
main.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
import subprocess
import sys
def list_to_format_str(libs_to_install: list) -> str:
"""
Converts a list of library names into a single space-separated string.
Args:
libs_to_install (list): List of library names to install.
Returns:
str: A single string containing all library names separated by spaces.
"""
return ' '.join(map(str, libs_to_install))
def libs_download(libs_to_install: list, suppress_output: bool = True):
"""
Installs the given list of Python libraries using pip.
Args:
libs_to_install (list): List of library names to install.
suppress_output (bool): If True, suppresses pip's output (stdout and stderr).
If False, displays pip's output in the console.
Raises:
subprocess.CalledProcessError: If the pip command fails.
"""
# Convert list of libraries to a space-separated string
str_libs_to_install = list_to_format_str(libs_to_install)
# Prepare command for pip installation
command = [sys.executable, '-m', 'pip', 'install'] + libs_to_install
# Redirect output based on suppress_output flag
stdout = subprocess.DEVNULL if suppress_output else None
stderr = subprocess.DEVNULL if suppress_output else None
# Execute the pip command
subprocess.check_call(command, stdout=stdout, stderr=stderr)
if __name__ == "__main__":
# Example usage
print("Welcome to the library installer!")
# Ask the user for a comma-separated list of libraries
libs = ['pyperclip', 'requests']
# Ask the user if they want to suppress output
suppress = True
try:
# Download and install libraries
libs_download(libs, suppress_output=suppress)
print("Libraries installed successfully!")
except subprocess.CalledProcessError as e:
print(f"An error occurred while installing libraries: {e}")