Skip to content

Commit 7b3dfb8

Browse files
authored
Merge pull request #1639 from microsoft/add_entry_point
Add debugpy entry point
2 parents 820d21e + aaab993 commit 7b3dfb8

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,7 @@ def tail_is(*suffixes):
195195
ext_modules=ExtModules(),
196196
has_ext_modules=lambda: True,
197197
cmdclass=cmds,
198+
# allow the user to call "debugpy" instead of "python -m debugpy"
199+
entry_points={"console_scripts": ["debugpy = debugpy.server.cli:main"]},
198200
**extras
199201
)

src/debugpy/__main__.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,66 @@
44

55
import sys
66

7-
87
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.
1327
#
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
1647
# to import debugpy/debugger.py.
1748
#
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:
2255
#
2356
# python /foo/bar/debugpy ...
2457
#
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.
3062
if "debugpy" not in sys.modules:
63+
3164
# Do not use dirname() to walk up - this can be a relative path, e.g. ".".
3265
sys.path[0] = sys.path[0] + "/../"
3366
import debugpy # noqa
34-
3567
del sys.path[0]
3668

3769
from debugpy.server import cli

0 commit comments

Comments
 (0)