()
| 115 | _all_external_lazy_imports.add(name) |
| 116 | |
| 117 | def import_mod(): |
| 118 | from polygraphy import config |
| 119 | from polygraphy.logger import G_LOGGER, LogMode |
| 120 | |
| 121 | def install_mod(install_name, install_version, raise_error=True): |
| 122 | modname = install_name.split(".")[0] |
| 123 | pkg = pkg_name if pkg_name is not None else _PKG_NAME_FROM_MODULE.get(modname, modname) |
| 124 | extra_flags = install_flags if install_flags is not None else _EXTRA_FLAGS_FOR_MODULE.get(modname, []) |
| 125 | |
| 126 | def fail(): |
| 127 | log_func = G_LOGGER.critical if raise_error else G_LOGGER.warning |
| 128 | log_func(f"Could not automatically install required module: {pkg}. Please install it manually.") |
| 129 | |
| 130 | if config.ASK_BEFORE_INSTALL: |
| 131 | res = None |
| 132 | while res not in ["y", "n"]: |
| 133 | res = input(f"Automatically install '{pkg}' (version: {install_version or 'any'}) ([Y]/n)? ") |
| 134 | res = res.strip()[:1].lower() or "y" |
| 135 | |
| 136 | if res == "n": |
| 137 | fail() |
| 138 | |
| 139 | if install_version == LATEST_VERSION: |
| 140 | extra_flags.append("--upgrade") |
| 141 | elif install_version is not None: |
| 142 | pkg += install_version |
| 143 | |
| 144 | cmd = config.INSTALL_CMD + [pkg] + extra_flags |
| 145 | G_LOGGER.info(f"Running installation command: {' '.join(cmd)}") |
| 146 | status = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE) |
| 147 | if status.returncode != 0: |
| 148 | G_LOGGER.error(f"Error during installation:\n{constants.TAB}{status.stderr.decode()}") |
| 149 | fail() |
| 150 | |
| 151 | mod = importlib.import_module(install_name) |
| 152 | return mod |
| 153 | |
| 154 | mod = None |
| 155 | try: |
| 156 | mod = importlib.import_module(name) |
| 157 | except ImportError as err: |
| 158 | if config.AUTOINSTALL_DEPS: |
| 159 | for install_name, install_version in all_required_mods: |
| 160 | G_LOGGER.info( |
| 161 | f"Module: '{install_name}' is required, but not installed. Attempting to install now." |
| 162 | ) |
| 163 | mod = install_mod(install_name, install_version) |
| 164 | else: |
| 165 | G_LOGGER.critical( |
| 166 | f"Module: '{name}' is required but could not be imported.\nNote: Error was: {err}\n" |
| 167 | f"You can set POLYGRAPHY_AUTOINSTALL_DEPS=1 in your environment variables to allow " |
| 168 | f"Polygraphy to automatically install missing modules.\n" |
| 169 | ) |
| 170 | |
| 171 | # Auto-upgrade if necessary |
| 172 | for install_name, install_version in all_required_mods: |
| 173 | installed_mod = importlib.import_module(install_name) |
| 174 | if ( |
no test coverage detected