7from optparse
import SUPPRESS_HELP, Values
8from typing
import List, Optional
30 check_legacy_setup_py_options,
36 check_externally_managed,
39 protect_pip_from_modification_on_windows,
44 running_under_virtualenv,
49logger = getLogger(__name__)
54 Install packages from:
56 - PyPI (and other indexes) using requirement specifiers.
58 - Local project directories.
59 - Local or remote source archives.
61 pip also supports installing from "requirements files", which provide
62 an easy way to specify a whole environment to be installed.
66 %prog [options] <requirement specifier> [package-index-options] ...
67 %prog [options] -r <requirements file> [package-index-options] ...
68 %prog [options] [-e] <vcs project url> ...
69 %prog [options] [-e] <local project path> ...
70 %prog [options] <archive url/path> ..."""
85 "Don't actually install anything, just print what would be. "
86 "Can be used in combination with --ignore-installed "
87 "to 'resolve' the requirements."
97 "Install packages into <dir>. "
98 "By default this will not replace existing files/folders in "
99 "<dir>. Use --upgrade to replace existing packages in <dir> "
107 dest=
"use_user_site",
110 "Install to the Python user install directory for your "
111 "platform. Typically ~/.local/, or %APPDATA%\\Python on "
112 "Windows. (See the Python documentation for site.USER_BASE "
118 dest=
"use_user_site",
119 action=
"store_false",
127 help=
"Install everything relative to this alternate root directory.",
135 "Installation prefix where lib, bin and other top-level "
136 "folders are placed. Note that the resulting installation may "
137 "contain scripts and other resources which reference the "
138 "Python interpreter of pip, and not that of ``--prefix``. "
139 "See also the ``--python`` option if the intention is to "
140 "install packages into another (possibly pip-free) "
153 "Upgrade all specified packages to the newest available "
154 "version. The handling of dependencies depends on the "
155 "upgrade-strategy used."
160 "--upgrade-strategy",
161 dest=
"upgrade_strategy",
162 default=
"only-if-needed",
163 choices=[
"only-if-needed",
"eager"],
165 "Determines how dependency upgrading should be handled "
166 "[default: %default]. "
167 '"eager" - dependencies are upgraded regardless of '
168 "whether the currently installed version satisfies the "
169 "requirements of the upgraded package(s). "
170 '"only-if-needed" - are upgraded only when they do not '
171 "satisfy the requirements of the upgraded package(s)."
177 dest=
"force_reinstall",
179 help=
"Reinstall all packages even if they are already up-to-date.",
184 "--ignore-installed",
185 dest=
"ignore_installed",
188 "Ignore the installed packages, overwriting them. "
189 "This can break your system if the existing package "
190 "is of a different version or was installed "
191 "with a different package manager!"
210 help=
"Compile Python source files to bytecode",
215 action=
"store_false",
217 help=
"Do not compile Python source files to bytecode",
221 "--no-warn-script-location",
222 action=
"store_false",
223 dest=
"warn_script_location",
225 help=
"Do not warn when installing scripts outside PATH",
228 "--no-warn-conflicts",
229 action=
"store_false",
230 dest=
"warn_about_conflicts",
232 help=
"Do not warn about broken dependencies",
251 dest=
"json_report_file",
255 "Generate a JSON file describing what pip did to install "
256 "the provided requirements. "
257 "Can be used in combination with --dry-run and --ignore-installed "
258 "to 'resolve' the requirements. "
259 "When - is used as file name it writes to stdout. "
260 "When writing to stdout, please combine with the --quiet option "
261 "to avoid mixing pip logging output with JSON output."
266 def run(self, options: Values, args: List[str]) -> int:
268 raise CommandError(
"Can not combine '--user' and '--target'")
275 installing_into_current_environment = (
282 installing_into_current_environment
285 check_externally_managed()
287 upgrade_strategy =
"to-satisfy-only"
302 target_temp_dir: Optional[TempDirectory] =
None
303 target_temp_dir_path: Optional[str] =
None
314 "Target path exists but is not a directory, will not continue."
326 target_python = make_target_python(options)
330 target_python=target_python,
338 globally_managed=
True,
343 check_legacy_setup_py_options(options, reqs)
354 temp_build_dir=directory,
356 build_tracker=build_tracker,
366 wheel_cache=wheel_cache,
371 upgrade_strategy=upgrade_strategy,
393 would_install_items = sorted(
397 if would_install_items:
400 " ".join(
"-".join(item)
for item
in would_install_items),
407 modifying_pip =
False
412 protect_pip_from_modification_on_windows(modifying_pip=modifying_pip)
417 if should_build_for_install_command(r)
420 _, build_failures = build(
422 wheel_cache=wheel_cache,
425 global_options=global_options,
430 "Could not build wheels for {}, which is required to "
431 "install pyproject.toml-based projects".format(
432 ", ".join(
r.name for r
in build_failures)
439 conflicts: Optional[ConflictDetails] =
None
440 should_warn_about_conflicts = (
443 if should_warn_about_conflicts:
450 warn_script_location =
False
452 installed = install_given_reqs(
456 home=target_temp_dir_path,
458 warn_script_location=warn_script_location,
465 home=target_temp_dir_path,
470 env = get_environment(lib_locations)
474 for result
in installed:
478 if installed_dist
is not None:
479 item = f
"{item}-{installed_dist.version}"
484 if conflicts
is not None:
490 installed_desc =
" ".join(items)
493 "Successfully installed %s",
496 except OSError
as error:
509 assert target_temp_dir
514 warn_if_run_as_root()
518 self, target_dir: str, target_temp_dir: TempDirectory, upgrade: bool
520 ensure_dir(target_dir)
540 for lib_dir
in lib_dir_list:
542 if lib_dir == data_dir:
550 "Target directory %s already exists. Specify "
551 "--upgrade to force replacement.",
557 "Target directory %s already exists and is "
558 "a link. pip will not automatically replace "
559 "links, please remove if replacement is "
572 self, to_install: List[InstallRequirement]
573 ) -> Optional[ConflictDetails]:
575 return check_install_conflicts(to_install)
578 "Error while checking for conflicts. Please file an issue on "
579 "pip's issue tracker: https://github.com/pypa/pip/issues/new"
584 self, conflict_details: ConflictDetails, resolver_variant: str
586 package_set, (missing, conflicting) = conflict_details
587 if not missing
and not conflicting:
590 parts: List[str] = []
591 if resolver_variant ==
"legacy":
593 "pip's legacy dependency resolver does not consider dependency "
594 "conflicts when selecting packages. This behaviour is the "
595 "source of the following dependency conflicts."
598 assert resolver_variant ==
"2020-resolver"
600 "pip's dependency resolver does not currently take into account "
601 "all the packages that are installed. This behaviour is the "
602 "source of the following dependency conflicts."
606 for project_name
in missing:
607 version = package_set[project_name][0]
608 for dependency
in missing[project_name]:
610 "{name} {version} requires {requirement}, "
611 "which is not installed."
615 requirement=dependency[1],
619 for project_name
in conflicting:
620 version = package_set[project_name][0]
621 for dep_name, dep_version, req
in conflicting[project_name]:
623 "{name} {version} requires {requirement}, but {you} have "
624 "{dep_name} {dep_version} which is incompatible."
630 dep_version=dep_version,
631 you=(
"you" if resolver_variant ==
"2020-resolver" else "you'll"),
640 home: Optional[str] =
None,
641 root: Optional[str] =
None,
642 isolated: bool =
False,
643 prefix: Optional[str] =
None,
664 use_user_site: Optional[bool],
665 prefix_path: Optional[str] =
None,
666 target_dir: Optional[str] =
None,
667 root_path: Optional[str] =
None,
668 isolated_mode: bool =
False,
670 """Determine whether to do a user install based on the input options.
672 If use_user_site is False, no additional checks are done.
673 If use_user_site is True, it is checked for compatibility with other
675 If use_user_site is None, the default behaviour depends on the environment,
676 which is provided by the other arguments.
680 if (use_user_site
is not None)
and (
not use_user_site):
687 "Can not combine '--user' and '--prefix' as they imply "
688 "different installation locations"
690 if virtualenv_no_global():
692 "Can not perform a '--user' install. User site-packages "
693 "are not visible in this virtualenv."
699 assert use_user_site
is None
702 if prefix_path
or target_dir:
703 logger.debug(
"Non-user install due to --prefix or --target option")
708 logger.debug(
"Non-user install because user site-packages disabled")
714 logger.debug(
"Non-user install because site-packages writeable")
718 "Defaulting to user installation because normal site-packages "
725 error: OSError, show_traceback: bool, using_user_site: bool
727 """Format an error message for an OSError
729 It may occur anytime during the execution of the install command.
734 parts.append(
"Could not install packages due to an OSError")
735 if not show_traceback:
747 user_option_part =
"Consider using the `--user` option"
748 permissions_part =
"Check the permissions"
750 if not running_under_virtualenv()
and not using_user_site:
771 "HINT: This error might have occurred since "
772 "this system does not have Windows Long Path "
773 "support enabled. You can find information on "
774 "how to enable this at "
775 "https://pip.pypa.io/warnings/enable-long-paths\n"
778 return "".join(parts).strip() +
"\n"
_T enter_context(self, ContextManager[_T] context_provider)
List[InstallRequirement] get_requirements(self, List[str] args, Values options, PackageFinder finder, PipSession session)
PackageFinder _build_package_finder(self, Values options, PipSession session, Optional[TargetPython] target_python=None, Optional[bool] ignore_requires_python=None)
None trace_basic_info(PackageFinder finder)
str determine_resolver_variant(Values options)
BaseResolver make_resolver(cls, RequirementPreparer preparer, PackageFinder finder, Values options, Optional[WheelCache] wheel_cache=None, bool use_user_site=False, bool ignore_installed=True, bool ignore_requires_python=False, bool force_reinstall=False, str upgrade_strategy="to-satisfy-only", Optional[bool] use_pep517=None, Optional[Tuple[int,...]] py_version_info=None)
RequirementPreparer make_requirement_preparer(cls, TempDirectory temp_build_dir, Values options, BuildTracker build_tracker, PipSession session, PackageFinder finder, bool use_user_site, Optional[str] download_dir=None, int verbosity=0)
PipSession get_default_session(self, Values options)
None _warn_about_conflicts(self, ConflictDetails conflict_details, str resolver_variant)
None _handle_target_dir(self, str target_dir, TempDirectory target_temp_dir, bool upgrade)
Optional[ConflictDetails] _determine_conflicts(self, List[InstallRequirement] to_install)
int run(self, Values options, List[str] args)
List[str] get_lib_location_guesses(bool user=False, Optional[str] home=None, Optional[str] root=None, bool isolated=False, Optional[str] prefix=None)
bool site_packages_writable(Optional[str] root, bool isolated)
str create_os_error_message(OSError error, bool show_traceback, bool using_user_site)
bool decide_user_install(Optional[bool] use_user_site, Optional[str] prefix_path=None, Optional[str] target_dir=None, Optional[str] root_path=None, bool isolated_mode=False)