如何在 intellij 中调试 python 容器?

How do I debug a python container in intellij?

docker 插件有一个用于连接到容器的调试端口

我有一个 python 应用程序,但根据 docs,只有 java 支持调试端口。

如何在 intellij 中设置断点和调试我的 python 容器?有什么方法可以让 python 容器连接到 intellij python 调试器吗?

编辑:我是 运行 Windows 10,docker for Windows,容器是 linux。也许我需要为 intellij Python 调试器手动设置某种远程调试?另外,不妨问一下,我必须要专业版才能进行远程调试吗?或者有使用社区的解决方法吗?

您可以使用 Python 远程调试来做到这一点。打开配置 window 然后点击 + -> Python Remote Debug

然后您要么设置一个端口,要么将其留空以便 Pycharm 找到可用的端口。

然后单击“调试”图标启动调试服务器,它将显示以下消息

Starting debug server at port 57588
Use the following code to connect to the debugger:
import pydevd
pydevd.settrace('localhost', port=57588, stdoutToServer=True, stderrToServer=True)
Waiting for process connection...

现在您需要在 docker 中设置 pydev 调试。为此,您将需要 pycharm-debug-py3k.egg。对我来说,我复制到我当前的 Dockerfile 文件夹,如下所示

cp "/Users/tarun.lalwani/Library/Application Support/IntelliJIdea2017.2/python/pycharm-debug-py3k.egg" .

您的位置将根据安装的 IntelliJ 版本而变化。之后,我们需要编辑我们的 Dockerfile

FROM python:3.6
WORKDIR /app
ENV PYTHONPATH=/app:/app/debug
COPY pycharm-debug-py3k.egg /app/debug
COPY debug_test.py /app/
CMD python debug_test.py

构建时的 debug_test.py 将在顶部显示以下行

import pydevd
pydevd.settrace('docker.for.mac.localhost', port=55507, stdoutToServer=True, stderrToServer=True)

Note: I have used docker.for.mac.localhost as I use docker for mac, but if use Docker for windows then use docker.for.win.localhost. For toolbox or linux you will add the IP of your machine

因为它是 docker,我们可能希望保持端口固定而不是像我那样动态。现在我们构建 docker 文件并 运行 它。

这将在 pycharm 中打开一个弹出窗口,单击自动检测以检测源映射

然后您将在文件的主行处设置代码断点