
# Module importieren
import os
import sys
import re
import subprocess
# Funktion zum Finden aller Python-Dateien in einem angegebenen Pfad definieren
def find_py_files(path):
# Leere Liste zum Speichern der Dateinamen initialisieren
py_files = []
# Durch alle Dateien und Verzeichnisse im Pfad iterieren
for root, dirs, files in os.walk(path):
# Durch alle Dateien iterieren
for file in files:
# Überprüfen, ob die Datei die Endung .py hat
if file.endswith(".py"):
# Den vollständigen Dateipfad zur Liste hinzufügen
py_files.append(os.path.join(root, file))
# Liste der Python-Dateien zurückgeben
return py_files
# Funktion zum Extrahieren aller importierten Module aus einer Python-Datei definieren
def extract_modules(file):
# Leere Menge zum Speichern der Modulnamen initialisieren
modules = set()
# Die Datei öffnen und den Inhalt lesen
with open(file, "r") as f:
content = f.read()
# Mit regulären Ausdrücken alle Import-Anweisungen finden
imports = re.findall(r"(?:from|import)\s+(\w+)", content)
# Durch alle Imports iterieren
for imp in imports:
# Den Modulnamen zur Menge hinzufügen
modules.add(imp)
# Menge der Module zurückgeben
return modules
# Funktion zum Überprüfen, ob ein Modul installiert ist oder nicht definieren
def is_installed(module):
# Versuchen, das Modul zu importieren
try:
__import__(module)
# Wenn keine Ausnahme ausgelöst wird, ist das Modul installiert
return True
except ImportError:
# Wenn eine Ausnahme ausgelöst wird, ist das Modul nicht installiert
return False
# Funktion zum Installieren eines Moduls mit pip definieren
def install_module(module):
# Subprozess verwenden, um den Befehl "pip install" auszuführen
subprocess.run([sys.executable, "-m", "pip", "install", module])
# Alle Laufwerke im System abrufen
drives = [f"{d}:\\" for d in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if os.path.exists(f"{d}:\\")]
# Durch alle Laufwerke iterieren
for drive in drives:
# Alle Python-Dateien im Laufwerk finden
py_files = find_py_files(drive)
# Durch alle Python-Dateien iterieren
for py_file in py_files:
# Alle importierten Module aus der Datei extrahieren
modules = extract_modules(py_file)
# Durch alle Module iterieren
for module in modules:
# Überprüfen, ob das Modul installiert ist oder nicht
if not is_installed(module):
# Das Modul installieren, falls es nicht installiert ist
install_module(module)
Oder ?
# Import modules
import os
import sys
import re
import subprocess
# Define a function to find all Python files in a given path
def find_py_files(path):
# Initialize an empty list to store the file names
py_files = []
# Loop through all the files and directories in the path
for root, dirs, files in os.walk(path):
# Loop through all the files
for file in files:
# Check if the file has a .py extension
if file.endswith(".py"):
# Append the full file path to the list
py_files.append(os.path.join(root, file))
# Return the list of Python files
return py_files
# Define a function to extract all the modules imported in a Python file
def extract_modules(file):
# Initialize an empty set to store the module names
modules = set()
# Open the file and read its contents
with open(file, "r") as f:
content = f.read()
# Use a regular expression to find all the import statements
imports = re.findall(r"(?:from|import)\s+(\w+)", content)
# Loop through all the imports
for imp in imports:
# Add the module name to the set
modules.add(imp)
# Return the set of modules
return modules
# Define a function to check if a module is installed or not
def is_installed(module):
# Try to import the module
try:
__import__(module)
# If no exception is raised, the module is installed
return True
except ImportError:
# If an exception is raised, the module is not installed
return False
# Define a function to install a module using pip
def install_module(module):
# Use subprocess to run pip install command
subprocess.run([sys.executable, "-m", "pip", "install", module])
# Get all the drives in the system
drives = [f"{d}:\\" for d in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" if os.path.exists(f"{d}:\\")]
# Loop through all the drives
for drive in drives:
# Find all the Python files in the drive
py_files = find_py_files(drive)
# Loop through all the Python files
for py_file in py_files:
# Extract all the modules imported in the file
modules = extract_modules(py_file)
# Loop through all the modules
for module in modules:
# Check if the module is installed or not
if not is_installed(module):
# Install the module if not installed
install_module(module)