1 配置debug环境
1.1 步骤一:9步配置远程环境
- 成功解决 vscode远程调试python
- 如果单步跳过快捷键
F10
没有反应,可以“设置 – 键盘快捷方式” 重新设置一遍 Debug: 单步跳过
:随便设一个键,然后再设置回 F10.
- 拓展插件
- remote development: 远程服务器控制
- python (Microsoft): python编程
- Office Viewer(Markdown Editor): 一个窗口实现markdown文件编写与查看
- View Image for Python Debugging: debug时可以显示图片(具体怎么看要看下插件内的教程)
- Settings Sync: 自动同步vscode的设置,会推荐给您安装
- Chinese (Simplified) (简体中文) Language Pack for Visual Studio Code
- vscode-pdf
- Xmind Viewer
1.2 步骤二:代码相对路径设置(VS code 默认以项目根目录为初始路径)
How to run python interactive in current file’s directory in Visual Studio Code?
pydevd warnings in Visual Studio Code Debug Console
不想分析英文论坛,可以直接使用下面具体的解决方案:
- 勾选
Python > Terminal: Execute In File Dir
:点击左下角齿轮设置按钮 Ctrl + ,
搜索 python.terminal.execute In File Dir
并勾选;
- 菜单栏
运行(Run)
,选择打开配置(Open configuration)
,在configurations
花括号末尾一行加上逗号后换行并输入"cwd": "${fileDirname}"
{
// 使用 IntelliSense 了解相关属性。(Use IntelliSense to learn about possible attributes.)
// 悬停以查看现有属性的描述。(Hover to view descriptions of existing attributes.)
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 (For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387)
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true, // debug时只进入项目自带文件,不进入安装的包文件
"env": {"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "2"}, // 以防debug看变量时,由于加载时间长而显示warning
"cwd": "${fileDirname}" // 控制代码的初始路径为debug文件所在路径
}
]
}
1.3 步骤三:debug 时args参数传入
debug
时给参数类parser = argparse.ArgumentParser()
传入指定参数而无需修改类中指定的默认参数,也可以使用xxx.yaml
参数文件传入。同“步骤二”在debug配置文件中增加:
{
// 使用 IntelliSense 了解相关属性。(Use IntelliSense to learn about possible attributes.)
// 悬停以查看现有属性的描述。(Hover to view descriptions of existing attributes.)
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 (For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387)
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true, // debug时只进入项目自带文件,不进入安装的包文件
"env": {"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "2"}, // 不显示 pydevd warnings
"cwd": "${fileDirname}", // 控制代码的初始路径为debug文件所在路径
"args": ["./configs/xxx.yaml", // debug时传入的参数
"--a", "A",
"--b", "B"
]
}
]
}
1.4 可能的错误
过程试图写入的管道不存在
C:\Users\Administrator.ssh\known_hosts,尝试删除你要连接的服务器的IP,保险起见删前先备份。
2 日常使用技巧
2.1 远程使用服务器时,需重新下载插件
方法1:修改设置
因为: Microsoft is no longer supported as explained in this thread.
- File > Preferences > Settings > TYPE “python.language server”
- 将 ‘Microsoft’ 改成 ‘Pylance’
方法2:删除\改名备份缓存文件:
- 将文件夹
C:\Users\Administrator\AppData\Roaming\Code
重命名Code_backups
为进行备份,重启使用vscode
2.2 使用jupyter开发,并正常显示图片
- 安装插件:
Jupyter Keymap
- 设置渲染为
image/png
: ctrl+shift+p
——“首选项:打开用户设置”——输入plots——去掉勾选项:Jupyter: Generate SVGPlots——点击无法正常显示图片位置的按钮选中“image/png”——重新加载
2.3 github远程代码管理
- 配合vscode使用git: 新手(3)—VSCode上的GitHub实现代码管理
- 克隆别人的代码为自己所用:git上clone别人的项目怎么放在自己的git上
- fork别人的代码同步更新:如何把别人的项目修改后,提交到自己的 github,并同步更新
- 服务器配置github的key: 云服务器中 ssh key 管理与 github 配置
- gitconfig文件路径:
~\.gitconfig
2.4 Debug时导入
- 上级目录
import sys
sys.path.append("..")
- 上
N
级目录CURRENT_DIR = os.path.split(os.path.abspath(__file__))[0] # 当前目录
config_path = CURRENT_DIR.rsplit('/', 2)[0] # 上N级目录
sys.path.append(config_path)