123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import subprocess
- import sys
- import importlib
- def check_and_install_packages(packages):
- """
- Checks if the specified packages are installed, and if not, prompts the user
- to install them.
- Parameters:
- - packages: A list of dictionaries, each containing:
- - 'module_name': The module or package name to import.
- - 'attribute': (Optional) The attribute or class to check within the module.
- - 'install_name': The name used in the pip install command.
- - 'version': (Optional) Version constraint for the package.
- """
- for package in packages:
- module_name = package['module_name']
- attribute = package.get('attribute')
- install_name = package.get('install_name', module_name)
- version = package.get('version', '')
- try:
- # Attempt to import the module
- module = importlib.import_module(module_name)
- # If an attribute is specified, check if it exists
- if attribute:
- getattr(module, attribute)
- except (ImportError, AttributeError):
- user_input = input(
- f"This program requires '{module_name}'"
- f"{'' if not attribute else ' with attribute ' + attribute}, which is not installed or missing.\n"
- f"Do you want to install '{install_name}' now? (y/n): "
- )
- if user_input.strip().lower() == 'y':
- try:
- # Build the pip install command
- install_command = [sys.executable, "-m", "pip", "install"]
- if version:
- install_command.append(f"{install_name}{version}")
- else:
- install_command.append(install_name)
- subprocess.check_call(install_command)
- # Try to import again after installation
- module = importlib.import_module(module_name)
- if attribute:
- getattr(module, attribute)
- print(f"Successfully installed '{install_name}'.")
- except Exception as e:
- print(f"An error occurred while installing '{install_name}': {e}")
- sys.exit(1)
- else:
- print(f"The program requires '{install_name}' to run. Exiting...")
- sys.exit(1)
- # import subprocess
- # import sys
- # def check_and_install_packages(packages):
- # """
- # Checks if the specified packages are installed, and if not, prompts the user
- # to install them.
- # Parameters:
- # - packages: A list of dictionaries, each containing:
- # - 'import_name': The name used in the import statement.
- # - 'install_name': (Optional) The name used in the pip install command.
- # Defaults to 'import_name' if not provided.
- # - 'version': (Optional) Version constraint for the package.
- # """
- # for package in packages:
- # import_name = package['import_name']
- # install_name = package.get('install_name', import_name)
- # version = package.get('version', '')
- # try:
- # print(f"import {import_name}")
- # __import__(import_name)
- # print(f"imported {import_name}")
- # except ImportError:
- # user_input = input(
- # f"This program requires the '{import_name}' library, which is not installed.\n"
- # f"Do you want to install it now? (y/n): "
- # )
- # if user_input.strip().lower() == 'y':
- # try:
- # # Build the pip install command
- # install_command = [sys.executable, "-m", "pip", "install"]
- # if version:
- # install_command.append(f"{install_name}{version}")
- # else:
- # install_command.append(install_name)
- # subprocess.check_call(install_command)
- # __import__(import_name)
- # print(f"Successfully installed '{install_name}'.")
- # except Exception as e:
- # print(f"An error occurred while installing '{install_name}': {e}")
- # sys.exit(1)
- # else:
- # print(f"The program requires the '{import_name}' library to run. Exiting...")
- # sys.exit(1)
|