base.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. import site
  3. import sys
  4. import sysconfig
  5. import typing
  6. from pip._internal.utils import appdirs
  7. from pip._internal.utils.virtualenv import running_under_virtualenv
  8. # Application Directories
  9. USER_CACHE_DIR = appdirs.user_cache_dir("pip")
  10. # FIXME doesn't account for venv linked to global site-packages
  11. site_packages = sysconfig.get_path("purelib") # type: typing.Optional[str]
  12. def get_major_minor_version():
  13. # type: () -> str
  14. """
  15. Return the major-minor version of the current Python as a string, e.g.
  16. "3.7" or "3.10".
  17. """
  18. return "{}.{}".format(*sys.version_info)
  19. def get_src_prefix():
  20. # type: () -> str
  21. if running_under_virtualenv():
  22. src_prefix = os.path.join(sys.prefix, "src")
  23. else:
  24. # FIXME: keep src in cwd for now (it is not a temporary folder)
  25. try:
  26. src_prefix = os.path.join(os.getcwd(), "src")
  27. except OSError:
  28. # In case the current working directory has been renamed or deleted
  29. sys.exit("The folder you are executing pip from can no longer be found.")
  30. # under macOS + virtualenv sys.prefix is not properly resolved
  31. # it is something like /path/to/python/bin/..
  32. return os.path.abspath(src_prefix)
  33. try:
  34. # Use getusersitepackages if this is present, as it ensures that the
  35. # value is initialised properly.
  36. user_site = site.getusersitepackages() # type: typing.Optional[str]
  37. except AttributeError:
  38. user_site = site.USER_SITE