Skip to content

Commit bcab469

Browse files
committed
Allow the user to specify debugpy path via the environment
1 parent 853cc05 commit bcab469

1 file changed

Lines changed: 52 additions & 19 deletions

File tree

src/debugpy/__main__.py

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,67 @@
33
# for license information.
44

55
import sys
6-
6+
import os
77

88
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.
9+
10+
# There are three ways to run debugpy:
11+
#
12+
# 1. Installed as a module in the current environment (python -m debugpy ...)
13+
# 2. Run as a script from source code (python <repo_root>/src/debugpy ...)
14+
# 3. Installed as a module in a random directory
15+
#
16+
# -----
17+
#
18+
# In the first case, no extra work is needed. Importing debugpy will work as expected.
19+
# Also, running 'debugpy' instead of 'python -m debugpy' will work because of the entry point
20+
# defined in setup.py.
21+
#
22+
# -----
1323
#
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
24+
# In the second case, sys.path[0] is the one added automatically by Python for the directory
25+
# containing this file. 'import debugpy' will not work since we need the parent directory
26+
# of debugpy/ to be in sys.path, rather than debugpy/ itself. So we need to modify sys.path[0].
27+
# Running 'debugpy' will not work because the entry point is not defined in this case.
28+
#
29+
# -----
30+
#
31+
# In the third case, running 'python -m debugpy' will not work because the module is not installed
32+
# in any environment. Running 'python <path_to_debugpy>' will work, just like the second case.
33+
# But running 'debugpy' will not work because even though the entry point is defined,
34+
# that path is not in sys.path, so 'import debugpy' will fail. So just like in the second case,
35+
# we need to modify sys.path[0].
36+
#
37+
# -----
38+
#
39+
# If we modify sys.path, 'import debugpy' will work, but it will break other imports
40+
# because they will be resolved relative to debugpy/ - e.g. `import debugger` will try
1641
# to import debugpy/debugger.py.
1742
#
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:
43+
# To fix both problems, we need to do the following steps:
44+
# 1. Modify sys.path[0] to point at the parent directory of debugpy/ instead of debugpy/ itself.
45+
# 2. Import debugpy.
46+
# 3. Remove sys.path[0] so that it doesn't affect future imports.
47+
#
48+
# For example, suppose the user did:
2249
#
2350
# python /foo/bar/debugpy ...
2451
#
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.
52+
# At the beginning of this script, sys.path[0] will contain "/foo/bar/debugpy".
53+
# We want to replace it with "/foo/bar', then 'import debugpy', then remove the replaced entry.
54+
# The imported debugpy module will remain in sys.modules, and thus all future imports of it
55+
# or its submodules will resolve accordingly.
3056
if "debugpy" not in sys.modules:
31-
# Do not use dirname() to walk up - this can be a relative path, e.g. ".".
32-
sys.path[0] = sys.path[0] + "/../"
33-
import debugpy # noqa
57+
58+
# if the user has specified a path to the debugpy module, replace sys.path[0] with
59+
# the specified path. Otherwise, replace sys.path[0] with the parent directory of debugpy/
60+
debugpy_path = os.environ.get("DEBUGPY_PATH")
61+
if (debugpy_path is not None):
62+
sys.path[0] = debugpy_path
63+
else:
64+
# Do not use dirname() to walk up - this can be a relative path, e.g. ".".
65+
sys.path[0] = sys.path[0] + "/../"
66+
import debugpy # noqa
3467

3568
del sys.path[0]
3669

0 commit comments

Comments
 (0)