troubleshoot.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. """
  3. display environment information that is frequently
  4. used to troubleshoot installations of Jupyter or IPython
  5. """
  6. from __future__ import annotations
  7. import os
  8. import platform
  9. import subprocess
  10. import sys
  11. from typing import Any, Union
  12. def subs(cmd: Union[list[str], str]) -> str | None:
  13. """
  14. get data from commands that we need to run outside of python
  15. """
  16. try:
  17. stdout = subprocess.check_output(cmd) # noqa: S603
  18. return stdout.decode("utf-8", "replace").strip()
  19. except (OSError, subprocess.CalledProcessError):
  20. return None
  21. def get_data() -> dict[str, Any]:
  22. """
  23. returns a dict of various user environment data
  24. """
  25. env: dict[str, Any] = {}
  26. env["path"] = os.environ.get("PATH")
  27. env["sys_path"] = sys.path
  28. env["sys_exe"] = sys.executable
  29. env["sys_version"] = sys.version
  30. env["platform"] = platform.platform()
  31. # FIXME: which on Windows?
  32. if sys.platform == "win32":
  33. env["where"] = subs(["where", "jupyter"])
  34. env["which"] = None
  35. else:
  36. env["which"] = subs(["which", "-a", "jupyter"])
  37. env["where"] = None
  38. env["pip"] = subs([sys.executable, "-m", "pip", "list"])
  39. env["conda"] = subs(["conda", "list"])
  40. env["conda-env"] = subs(["conda", "env", "export"])
  41. return env
  42. def main() -> None:
  43. """
  44. print out useful info
  45. """
  46. # pylint: disable=superfluous-parens
  47. # args = get_args()
  48. if "_ARGCOMPLETE" in os.environ:
  49. # No arguments to complete, the script can be slow to run to completion,
  50. # so in case someone tries to complete jupyter troubleshoot just exit early
  51. return
  52. environment_data = get_data()
  53. print("$PATH:")
  54. for directory in environment_data["path"].split(os.pathsep):
  55. print(f"\t{directory}")
  56. print("\nsys.path:")
  57. for directory in environment_data["sys_path"]:
  58. print(f"\t{directory}")
  59. print("\nsys.executable:")
  60. print(f"\t{environment_data['sys_exe']}")
  61. print("\nsys.version:")
  62. if "\n" in environment_data["sys_version"]:
  63. for data in environment_data["sys_version"].split("\n"):
  64. print(f"\t{data}")
  65. else:
  66. print(f"\t{environment_data['sys_version']}")
  67. print("\nplatform.platform():")
  68. print(f"\t{environment_data['platform']}")
  69. if environment_data["which"]:
  70. print("\nwhich -a jupyter:")
  71. for line in environment_data["which"].split("\n"):
  72. print(f"\t{line}")
  73. if environment_data["where"]:
  74. print("\nwhere jupyter:")
  75. for line in environment_data["where"].split("\n"):
  76. print(f"\t{line}")
  77. if environment_data["pip"]:
  78. print("\npip list:")
  79. for package in environment_data["pip"].split("\n"):
  80. print(f"\t{package}")
  81. if environment_data["conda"]:
  82. print("\nconda list:")
  83. for package in environment_data["conda"].split("\n"):
  84. print(f"\t{package}")
  85. if environment_data["conda-env"]:
  86. print("\nconda env:")
  87. for package in environment_data["conda-env"].split("\n"):
  88. print(f"\t{package}")
  89. if __name__ == "__main__":
  90. main()