gpt4 book ai didi

python - 如何在 intellij 中调试 python 容器?

转载 作者:IT老高 更新时间:2023-10-28 21:25:28 28 4
gpt4 key购买 nike

docker 插件有一个用于连接容器的调试端口 enter image description here

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

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

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

最佳答案

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

Python Debugger Option

然后你要么设置一个端口,要么把它留空,让 Pycharm 找到一个可用的端口。

Python Remote Debug

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

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中打开一个弹出窗口,点击自动检测以检测源映射

Auto detect

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

Debug Local Execution Remote

关于python - 如何在 intellij 中调试 python 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659337/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com