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