我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用pip.commands.install.InstallCommand()。
def patch_setuptools(fetch_directives=('index_url', 'find_links')): orig = easy_install.finalize_options def patched_finalize_options(self): cmd = PipInstallCommand() config = cmd.parser.parse_args([])[0] for option in fetch_directives: try: value = getattr(config, option) except AttributeError: continue setattr(self, option, value) orig(self) easy_install.finalize_options = patched_finalize_options
def install_missing_requirements(module): """ Some of the modules require external packages to be installed. This gets the list from the `REQUIREMENTS` module attribute and attempts to install the requirements using pip. :param module: GPIO module :type module: ModuleType :return: None :rtype: NoneType """ reqs = getattr(module, "REQUIREMENTS", []) if not reqs: _LOG.info("Module %r has no extra requirements to install." % module) return import pkg_resources pkgs_installed = pkg_resources.WorkingSet() pkgs_required = [] for req in reqs: if pkgs_installed.find(pkg_resources.Requirement.parse(req)) is None: pkgs_required.append(req) if pkgs_required: from pip.commands.install import InstallCommand from pip.status_codes import SUCCESS cmd = InstallCommand() result = cmd.main(pkgs_required) if result != SUCCESS: raise CannotInstallModuleRequirements( "Unable to install packages for module %r (%s)..." % ( module, pkgs_required))
def install(params): """ Install third-party Mod """ from pip import main as pip_main from pip.commands.install import InstallCommand params = [param for param in params] options, mod_list = InstallCommand().parse_args(params) params = ["install"] + params for mod_name in mod_list: mod_name_index = params.index(mod_name) if mod_name in system_mod: print('System Mod can not be installed or uninstalled') return if "rqalpha_mod_" in mod_name: lib_name = mod_name mod_name = lib_name.replace("rqalpha_mod_", "") else: lib_name = "rqalpha_mod_" + mod_name params[mod_name_index] = lib_name # Install Mod pip_main(params) # Export config config_path = get_default_config_path() config = load_config(config_path, loader=yaml.RoundTripLoader) for mod_name in mod_list: if "rqalpha_mod_" in mod_name: lib_name = mod_name mod_name = lib_name.replace("rqalpha_mod_", "") else: lib_name = "rqalpha_mod_" + mod_name mod = import_module(lib_name) mod_config = yaml.load(mod.__mod_config__, yaml.RoundTripLoader) config['mod'][mod_name] = mod_config config['mod'][mod_name]['lib'] = lib_name config['mod'][mod_name]['enabled'] = False config['mod'][mod_name]['priority'] = 1000 dump_config(config_path, config)
def bootstrap(tmpdir=None): # Import pip so we can use it to install pip and maybe setuptools too import pip from pip.commands.install import InstallCommand # Wrapper to provide default certificate with the lowest priority class CertInstallCommand(InstallCommand): def parse_args(self, args): # If cert isn't specified in config or environment, we provide our # own certificate through defaults. # This allows user to specify custom cert anywhere one likes: # config, environment variable or argv. if not self.parser.get_default_values().cert: self.parser.defaults["cert"] = cert_path # calculated below return super(CertInstallCommand, self).parse_args(args) pip.commands_dict["install"] = CertInstallCommand # We always want to install pip packages = ["pip"] # Check if the user has requested us not to install setuptools if "--no-setuptools" in sys.argv or os.environ.get("PIP_NO_SETUPTOOLS"): args = [x for x in sys.argv[1:] if x != "--no-setuptools"] else: args = sys.argv[1:] # We want to see if setuptools is available before attempting to # install it try: import setuptools # noqa except ImportError: packages += ["setuptools"] delete_tmpdir = False try: # Create a temporary directory to act as a working directory if we were # not given one. if tmpdir is None: tmpdir = tempfile.mkdtemp() delete_tmpdir = True # We need to extract the SSL certificates from requests so that they # can be passed to --cert cert_path = os.path.join(tmpdir, "cacert.pem") with open(cert_path, "wb") as cert: cert.write(pkgutil.get_data("pip._vendor.requests", "cacert.pem")) # Execute the included pip and use it to install the latest pip and # setuptools from PyPI sys.exit(pip.main(["install", "--upgrade"] + packages + args)) finally: # Remove our temporary directory if delete_tmpdir and tmpdir: shutil.rmtree(tmpdir, ignore_errors=True)