gpt4 book ai didi

python - 来自 Django 的开发服务器端口绑定(bind)错误的 ptvsd 远程调试

转载 作者:行者123 更新时间:2023-12-02 19:25:41 25 4
gpt4 key购买 nike

Python 进程我们仅限于绑定(bind)到一个 IP 地址。尝试运行 python manage.py runserver添加 ptvsd 后行导致错误。有没有解决的办法?

Traceback (most recent call last):
File "manage.py", line 27, in <module>
main()
File "manage.py", line 14, in main
ptvsd.enable_attach(address=('0.0.0.0', 18080), redirect_output=True)
File "/usr/local/lib/python3.7/site-packages/ptvsd/attach_server.py", line 96, in enable_attach
ptvsd_enable_attach(address)
File "/usr/local/lib/python3.7/site-packages/ptvsd/_remote.py", line 75, in enable_attach
patch_multiprocessing=ptvsd.options.multiprocess)
File "/usr/local/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2017, in settrace
wait_for_ready_to_run,
File "/usr/local/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 2068, in _locked_settrace
debugger.connect(host, port) # Note: connect can raise error.
File "/usr/local/lib/python3.7/site-packages/ptvsd/_vendored/pydevd/pydevd.py", line 914, in connect
s = start_client(host, port)
File "/usr/local/lib/python3.7/site-packages/ptvsd/pydevd_hooks.py", line 132, in <lambda>
_start_client = (lambda h, p: start_client(daemon, h, p))
File "/usr/local/lib/python3.7/site-packages/ptvsd/_remote.py", line 58, in <lambda>
start_client=(lambda daemon, h, port: start_daemon()),
File "/usr/local/lib/python3.7/site-packages/ptvsd/_remote.py", line 50, in start_daemon
_, next_session = daemon.start_server(addr=(host, port))
File "/usr/local/lib/python3.7/site-packages/ptvsd/daemon.py", line 161, in start_server
self._server = create_server(addr.host, addr.port)
File "/usr/local/lib/python3.7/site-packages/ptvsd/socket.py", line 79, in create_server
server.bind((host, port))
OSError: [Errno 98] Address already in use

管理.py

#!/usr/bin/env python
import os
import sys
import dotenv

def main():
dotenv.read_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), '.app.env'), override=True)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
try:
# for debug
if os.environ['APP_MODE'] == 'DEBUG':
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 18080), redirect_output=True)

from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()

docker-compose.yml (摘抄)

command: sh -c "./wait-for-it.sh db:5432 && python manage.py runserver 0.0.0.0:$18080"

最佳答案

您可以在设置中添加它,而不是添加它 manage.py:

if DEBUG:
import ptvsd
import sys
if "runserver" in sys.argv and os.environ.get("RUN_MAIN"):
try:
# Only run debugger for the server.
ptvsd.enable_attach(redirect_output=True)
except OSError:
print("Could not enable debugging, disabling. Exception:")
traceback.print_exc()

默认端口为 5678。
这是我的 docker-compose 内容:
version: '3.7'

services:
db:
image: postgres
web:
build: .
container_name: beHired_dev
volumes:
- .:/code
ports:
- "8000:8000"
- "5678:5678"
depends_on:
- db
env_file: .env
environment:
SECRET_KEY: ${SECRET_KEY}
DJANGO_SETTINGS_MODULE: ${DJANGO_SETTINGS_MODULE}

如果您使用的是 VSCode:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "beHired_dev",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/code"
}
]
}
]
}

关于python - 来自 Django 的开发服务器端口绑定(bind)错误的 ptvsd 远程调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56459514/

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