install_packages.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import subprocess
  2. import sys
  3. import importlib
  4. def check_and_install_packages(packages):
  5. """
  6. Checks if the specified packages are installed, and if not, prompts the user
  7. to install them.
  8. Parameters:
  9. - packages: A list of dictionaries, each containing:
  10. - 'module_name': The module or package name to import.
  11. - 'attribute': (Optional) The attribute or class to check within the module.
  12. - 'install_name': The name used in the pip install command.
  13. - 'version': (Optional) Version constraint for the package.
  14. """
  15. for package in packages:
  16. module_name = package['module_name']
  17. attribute = package.get('attribute')
  18. install_name = package.get('install_name', module_name)
  19. version = package.get('version', '')
  20. try:
  21. # Attempt to import the module
  22. module = importlib.import_module(module_name)
  23. # If an attribute is specified, check if it exists
  24. if attribute:
  25. getattr(module, attribute)
  26. except (ImportError, AttributeError):
  27. user_input = input(
  28. f"This program requires '{module_name}'"
  29. f"{'' if not attribute else ' with attribute ' + attribute}, which is not installed or missing.\n"
  30. f"Do you want to install '{install_name}' now? (y/n): "
  31. )
  32. if user_input.strip().lower() == 'y':
  33. try:
  34. # Build the pip install command
  35. install_command = [sys.executable, "-m", "pip", "install"]
  36. if version:
  37. install_command.append(f"{install_name}{version}")
  38. else:
  39. install_command.append(install_name)
  40. subprocess.check_call(install_command)
  41. # Try to import again after installation
  42. module = importlib.import_module(module_name)
  43. if attribute:
  44. getattr(module, attribute)
  45. print(f"Successfully installed '{install_name}'.")
  46. except Exception as e:
  47. print(f"An error occurred while installing '{install_name}': {e}")
  48. sys.exit(1)
  49. else:
  50. print(f"The program requires '{install_name}' to run. Exiting...")
  51. sys.exit(1)
  52. # import subprocess
  53. # import sys
  54. # def check_and_install_packages(packages):
  55. # """
  56. # Checks if the specified packages are installed, and if not, prompts the user
  57. # to install them.
  58. # Parameters:
  59. # - packages: A list of dictionaries, each containing:
  60. # - 'import_name': The name used in the import statement.
  61. # - 'install_name': (Optional) The name used in the pip install command.
  62. # Defaults to 'import_name' if not provided.
  63. # - 'version': (Optional) Version constraint for the package.
  64. # """
  65. # for package in packages:
  66. # import_name = package['import_name']
  67. # install_name = package.get('install_name', import_name)
  68. # version = package.get('version', '')
  69. # try:
  70. # print(f"import {import_name}")
  71. # __import__(import_name)
  72. # print(f"imported {import_name}")
  73. # except ImportError:
  74. # user_input = input(
  75. # f"This program requires the '{import_name}' library, which is not installed.\n"
  76. # f"Do you want to install it now? (y/n): "
  77. # )
  78. # if user_input.strip().lower() == 'y':
  79. # try:
  80. # # Build the pip install command
  81. # install_command = [sys.executable, "-m", "pip", "install"]
  82. # if version:
  83. # install_command.append(f"{install_name}{version}")
  84. # else:
  85. # install_command.append(install_name)
  86. # subprocess.check_call(install_command)
  87. # __import__(import_name)
  88. # print(f"Successfully installed '{install_name}'.")
  89. # except Exception as e:
  90. # print(f"An error occurred while installing '{install_name}': {e}")
  91. # sys.exit(1)
  92. # else:
  93. # print(f"The program requires the '{import_name}' library to run. Exiting...")
  94. # sys.exit(1)