vscode 使用笔记

vscode 使用笔记

  • 1 debug环境
    • 步骤一:9步配置远程环境
    • 步骤二:代码相对路径设置(VS code 默认以项目根目录为初始路径)
    • 步骤三:debug 时args参数传入
    • 可能的错误
  • 2 使用vs code过程中遇过的问题
    • 2.1 远程使用服务器时,需重新下载插件
    • 2.2 使用jupyter开发,并正常显示图片
    • 2.3 github远程代码管理
    • 2.4 Debug时导入不同级别路径
    • 2.5 文件、代码对齐线
    • 2.6 正在加载... 和 F12无法定位到函数定义
    • 2.7 macOs收起左侧文件树
    • 2.8 文件修改在文件树、文件内无标记
    • 2.9 GLIBCXX_3.4.29 not found
    • 2.10 pydevd warning: Getting attribute ndarray.imag was slow (took 82.74s)
  • 3 便捷操作
    • 3.1 vscode使用快捷键
    • 3.2 设置运行到光标处(Run to Cursor)快捷键

1 debug环境

步骤一:9步配置远程环境

  • 成功解决 vscode远程调试python
  • 如果单步跳过快捷键F10没有反应,可以“设置 – 键盘快捷方式” 重新设置一遍 Debug: 单步跳过:随便设一个键,然后再设置回 F10.
  • 拓展插件
    • remote development: 远程服务器控制
    • remote - SSH: 远程使用shell
    • 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

步骤二:代码相对路径设置(VS code 默认以项目根目录为初始路径)

How to run python interactive in current file’s directory in Visual Studio Code?
pydevd warnings in Visual Studio Code Debug Console

不想分析英文论坛,可以直接使用下面具体的解决方案:

  1. 勾选Python > Terminal: Execute In File Dir:点击左下角齿轮设置按钮 Ctrl + , 搜索 python.terminal.execute In File Dir 并勾选;
  2. 菜单栏运行(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文件所在路径
           }
       ]
    }
    

步骤三: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"
	           ]         
	       }
	   ]
	}

可能的错误

过程试图写入的管道不存在
C:\Users\Administrator.ssh\known_hosts,尝试删除你要连接的服务器的IP,保险起见删前先备份。

2 使用vs code过程中遇过的问题

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开发,并正常显示图片

  1. 安装插件:Jupyter Keymap
  2. 设置渲染为image/png: ctrl+shift+p——“首选项:打开用户设置”——输入plots——去掉勾选项:Jupyter: Generate SVGPlots——点击无法正常显示图片位置的按钮选中“image/png”——重新加载

2.3 github远程代码管理

使用源代码管理,先点+stage changes(相当于git add [文件名]),再提交(相当远git commit -a)。具体参考:https://zhuanlan.zhihu.com/p/34753075

问题:每次提交都会先输出文件,再文件中输入comment才能完成commit

左下角设置,搜索设置输入use Editor As commit input去掉勾选框

保留后期可参考的博客

  1. 配合vscode使用git: 新手(3)—VSCode上的GitHub实现代码管理
  2. 克隆别人的代码为自己所用:git上clone别人的项目怎么放在自己的git上
  3. fork别人的代码同步更新:如何把别人的项目修改后,提交到自己的 github,并同步更新
  4. 服务器配置github的key: 云服务器中 ssh key 管理与 github 配置
  5. gitconfig文件路径:~\.gitconfig

2.4 Debug时导入不同级别路径

  1. 上级目录
    import sys
    sys.path.append("..") 
    
  2. N级目录
    CURRENT_DIR = os.path.split(os.path.abspath(__file__))[0]  # 当前目录
    config_path = CURRENT_DIR.rsplit('/', 2)[0]  # 上N级目录
    sys.path.append(config_path)
    
  3. 创建路径(如果路径tmp_path不存在 则自动创建)
    n = 1
    tmp_path = './dir/tmp_{}'.format(n)
    os.makedirs(tmp_path , exist_ok=True)
    

2.5 文件、代码对齐线

文件对齐线:首选项——设置——搜索:renderIndentGuides,选择always
代码对齐线:更换颜色主题就会出现
vscode 使用笔记_第1张图片

2.6 正在加载… 和 F12无法定位到函数定义

鼠标放在函数上显示“正在加载…”,ctrl + 鼠标左键 或者 F12 无法定位到函数定义

大概率是扩展插件导致的,切换使用稍旧版本的插件:右键插件——安装另一版本…

2.7 macOs收起左侧文件树

快捷键收起资源管理器:command + b

2.8 文件修改在文件树、文件内无标记

设置下搜索Compact Folders,取消勾选。
vscode 1.76版本以后在文件内的标记修改取消了,可以降级到1.75版本快速修复;参考:VS Code highlight modified lines

2.9 GLIBCXX_3.4.29 not found

ImportError: /lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29’ not found (required by /home/wuye/anaconda3/envs/tf2/lib/python3.8/site-packages/google/protobuf/pyext/_message.cpython-38-x86_64-linux-gnu.so)

这个是默认路径下的libstdc++.so.6缺少GLIBCXX_3.4.29

# 确认路径下有哪些GLIBCXX
strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX
# 查找系统中所有的libstdc++.so.6
sudo find / -name "libstdc++.so.6*"

# 假设有一条为:/home/amax/anaconda3/envs/libo_tf/lib/libstdc++.so.6.0.29
# 查看该条下是否有GLIBCXX_3.4.29
strings /home/amax/anaconda3/envs/libo_tf/lib/libstdc++.so.6.0.29 | grep GLIBCXX
# 如果有的话就复制一份并建好软链接
# 复制
sudo cp /home/amax/anaconda3/envs/libo_tf/lib/libstdc++.so.6.0.29 /usr/lib/x86_64-linux-gnu/
# 删除之前链接
sudo rm /usr/lib/x86_64-linux-gnu/libstdc++.so.6
# 创建新的链接
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.29 /usr/lib/x86_64-linux-gnu/libstdc++.so.6

2.10 pydevd warning: Getting attribute ndarray.imag was slow (took 82.74s)

Customize report timeout by setting the PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT environment variable to a higher timeout (default is: 0.5s)
launch.json文件中加入

"env": {"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "2"}, 

3 便捷操作

3.1 vscode使用快捷键

【调试】
继续: F5
调试下一步(需要重新设置一遍):F10
单步跳入: F11
单步跳出:Shift F11
重新调试:Ctrl + Shift + F5
结束调试:Shift + F5
复制整行:SHIFT + ALT + UP/DOWN
添加监视(自行设置):Alt + F10
选择整行:CTRL + L
删除整行:CTRL + SHIFT + K
移动选中的行:ALT + UP/DOWN
批量更改变量(多按几次):CTRL + D
光标在任意位置换行:CTRL+ENTER
光标跳到下一个单词:CTRL + LEFT/RIGHT
代码折叠:ctrl k 再 ctrl 0(n任意层级)
代码展开:ctrl k 再 ctrl j

3.2 设置运行到光标处(Run to Cursor)快捷键

[win] File - Preference - Keyboard Shortcuts - 搜索 run to cursor - 设置快捷键(比如 Ctrl+F10)
[mac] code - 首选项 - 键盘快捷方式[command + K, command + S] - 搜索 run to cursor - 设置快捷键(比如 command+F10)

你可能感兴趣的:(Python,vscode)