之前嘗試過(guò)用vscode開(kāi)發(fā)python web。但是在設(shè)置virtualenv時(shí)出現(xiàn)問(wèn)題,網(wǎng)上查找原因是windows系統(tǒng)python在虛擬環(huán)境下不能逐行執(zhí)行,不過(guò)好像現(xiàn)在解決了?!
創(chuàng)建項(xiàng)目文件夾,打開(kāi)文件夾在空白處shift + 右鍵 打開(kāi)命令行窗口
在項(xiàng)目文件夾中使用virtualenv .env 創(chuàng)建.env文件夾
創(chuàng)建 requirements.txt 文件,文件中添加以下內(nèi)容,pylint為python的靜態(tài)語(yǔ)法檢測(cè)器,pylint-django 是適用于django項(xiàng)目的語(yǔ)法檢查其插件,autopep8 是代碼格式化工具
django
pylint
pylint-django
autopep8
執(zhí)行 .env\Scripts\activate.bat 激活虛擬環(huán)境
執(zhí)行 pip install -r requirements.txt 安裝相關(guān)模塊
執(zhí)行 django-admin startproject <project> . 在當(dāng)前目錄下創(chuàng)建django項(xiàng)目文件夾
在<project>中執(zhí)行 python manage.py startapp <app> 新建django app
在當(dāng)前路徑下執(zhí)行 code . 打開(kāi)vscode
在 vscode 中按下 Ctrl + Shift + P,輸入 select,選擇 Python: Select Workspace Interpreter,在出現(xiàn)的選項(xiàng)中將Python解析器指向env文件夾中的python

- 在 vscode 中按下 Ctrl + Shift + P,輸入 workspace,選擇 Preferences: Open Workspace Settings,配置完成后的JSON文件如下
{
"python.pythonPath": "e:web-server/.env/scripts/python.exe",
"python.linting.pylintPath": "pylint",
"python.linting.pylintArgs": [
"--load-plugins", "pylint_django"
],
"python.formatting.autopep8Path": "autopep8"
}
- 打開(kāi)調(diào)試,運(yùn)行,然后打開(kāi)lanuch.json,修改django的配置文件如下

{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceRoot}/demo/manage.py",
"cwd": "${workspaceRoot}",
"args": [
"runserver",
"--noreload"
],
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput",
"DjangoDebugging"
]
},
這樣即使退出虛擬環(huán)境,直接運(yùn)行vscode也可以debug了。