python - Visual Studio Code 使用 python3 在 docker 容器中调试 python 2.7 应用程序
问题描述
我正在尝试在 docker 容器中调试我的应用程序。该应用程序是用 paython 2.7 编写的,但 VS Code 尝试使用 python 3 对其进行调试。因此,它无法解析包并抛出异常。
任何想法?
我的配置:
任务.json
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"dockerRun": {
"containerName": "account",
"image": "account:latest",
"env": {},
"volumes": [
{
"containerPath": "/code",
"localPath": "${workspaceFolder}"
}
],
"ports": [
{
"containerPort": 8086,
"hostPort": 8086
},
{
"containerPort": 8085,
"hostPort": 8085
}
]
},
"python": {
"args": ["--config=/code/config/account.conf.localstage"],
"file": "skoobe-accountd"
}
},
{
"label": "docker-build",
"type": "docker-build",
"dockerBuild": {
"context": "${workspaceFolder}",
"dockerfile": "${workspaceFolder}/Dockerfile.arm.dev",
"tag": "account:latest"
}
}
]
}
启动.json
{
"configurations": [
{
"name": "Debug Account",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/code"
}
],
"projectType": "general"
}
}
]
}
设置.json
{
"python.linting.pylintEnabled": true,
"python.pythonPath": "/usr/bin/python"
}
Dockerfile
FROM python:2.7.18
ENV AWS_ACCESS_KEY_ID="xxx"
ENV AWS_SECRET_ACCESS_KEY="xxx"
ENV AWS_DEFAULT_REGION="xxx"
ENV SQS_QUEUE="xxx"
ENV SQS_CHANGE_QUEUE="xxx"
# RUN mkdir -p /root/.ssh && \
# chmod 0700 /root/.ssh
# RUN apt-get update && \
# apt-get install openssh-server -y
WORKDIR /code/
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
WORKDIR tools/
RUN python setup.py install
WORKDIR /code/
# RUN pip install -r requirements.txt
CMD ["python", "accountd", "--config=/code/config/account.conf.localstage"]
解决方案
经过一番调查,我找到了解决方案:
我修改lunch.json
为以下内容:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/code"
}
]
}
]
}
然后我将以下行添加到实体 Python 文件中:
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 5678))
从 VS 代码 IDE 运行 docker 容器后,我可以在调试模式下运行。
推荐阅读
- javascript - 调用多个 python 函数,而其中一些函数仍未完成
- python - AttributeError:“NoneType”对象没有属性“下载”
- javascript - 动态生成的深层对象
- python - deepfacelab ImportError: DLL load failed: 找不到指定的模块
- erlang - Erlang generate anonymous function of an arbitary arity?
- discord - 如何检测 BOT 是否在语音聊天中?不和谐.JS
- android - 如何使用压缩文件系统替换android系统镜像?
- python - XGBoost。GPU 用于训练 CPU 进行推理
- javascript - 在时刻 js 上使用 endOf('quarter') 后时区发生变化
- javascript - 如何在 Javascript 中保护 SQLite 本地数据库?