__main__.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License. See LICENSE in the project root
  3. # for license information.
  4. import sys
  5. if __name__ == "__main__":
  6. # There are three ways to run debugpy:
  7. #
  8. # 1. Installed as a module in the current environment (python -m debugpy ...)
  9. # 2. Run as a script from source code (python <repo_root>/src/debugpy ...)
  10. # 3. Installed as a module in a random directory
  11. #
  12. # -----
  13. #
  14. # In the first case, no extra work is needed. Importing debugpy will work as expected.
  15. # Also, running 'debugpy' instead of 'python -m debugpy' will work because of the entry point
  16. # defined in setup.py.
  17. #
  18. # -----
  19. #
  20. # In the second case, sys.path[0] is the one added automatically by Python for the directory
  21. # containing this file. 'import debugpy' will not work since we need the parent directory
  22. # of debugpy/ to be in sys.path, rather than debugpy/ itself. So we need to modify sys.path[0].
  23. # Running 'debugpy' will not work because the entry point is not defined in this case.
  24. #
  25. # -----
  26. #
  27. # In the third case, running 'python -m debugpy' will not work because the module is not installed
  28. # in any environment. Running 'python <install_dir>/debugpy' will work, just like the second case.
  29. # But running the entry point will not work because python doesn't know where to find the debugpy module.
  30. #
  31. # In this case, no changes to sys.path are required. You just have to do the following before calling
  32. # the entry point:
  33. # 1. Add <install_dir> to PYTHONPATH.
  34. # On Windows, this is set PYTHONPATH=%PYTHONPATH%;<install_dir>
  35. # 2. Add <install_dir>/bin to PATH. (OPTIONAL)
  36. # On Windows, this is set PATH=%PATH%;<install_dir>\bin
  37. # 3. Run the entry point from a command prompt
  38. # On Windows, this is <install_dir>\bin\debugpy.exe, or just 'debugpy' if you did the previous step.
  39. #
  40. # -----
  41. #
  42. # If we modify sys.path, 'import debugpy' will work, but it will break other imports
  43. # because they will be resolved relative to debugpy/ - e.g. `import debugger` will try
  44. # to import debugpy/debugger.py.
  45. #
  46. # To fix both problems, we need to do the following steps:
  47. # 1. Modify sys.path[0] to point at the parent directory of debugpy/ instead of debugpy/ itself.
  48. # 2. Import debugpy.
  49. # 3. Remove sys.path[0] so that it doesn't affect future imports.
  50. #
  51. # For example, suppose the user did:
  52. #
  53. # python /foo/bar/debugpy ...
  54. #
  55. # At the beginning of this script, sys.path[0] will contain "/foo/bar/debugpy".
  56. # We want to replace it with "/foo/bar', then 'import debugpy', then remove the replaced entry.
  57. # The imported debugpy module will remain in sys.modules, and thus all future imports of it
  58. # or its submodules will resolve accordingly.
  59. if "debugpy" not in sys.modules:
  60. # Do not use dirname() to walk up - this can be a relative path, e.g. ".".
  61. sys.path[0] = sys.path[0] + "/../"
  62. import debugpy # noqa
  63. del sys.path[0]
  64. from debugpy.server import cli
  65. cli.main()