VS Code python 使用笔记之 editing

版权声明:本文为博主原创文章,如能帮助到各位,荣幸之至,欢迎转载。 https://blog.csdn.net/m0_37639589/article/details/82529987

VS Code python 使用笔记之 editing

前言

上一篇将 tutorial 全文翻译,费时费力效果也不尽人意。于是今后这些学习笔记中只将重点部分归纳摘出。事实证明认真看帮助文档还是很有用的,学到了不少实用的技巧。

自动补全和智能感知

通过自定义包路径增强智能感知功能

对于那些安装在非标准位置的包,可以通过指定设置中 python.autoComplete.extraPaths 路径的值实现智能感知。例如,你将 Google App Engine 安装在自定义路径中,可以这样做:

Windows:

"python.autoComplete.extraPaths": [
    "C:/Program Files (x86)/Google/google_appengine",
    "C:/Program Files (x86)/Google/google_appengine/lib" ]

macOS/Linux:

"python.autoComplete.extraPaths": [
    "~/.local/lib/Google/google_appengine",
    "~/.local/lib/Google/google_appengine/lib" ]

python.autoComplete.preloadModules 设置可以通过预加载包信息加速指定包的自动补全功能。例如:

"python.autoComplete.preloadModules": ["numpy", "pandas", "matplotlib"],

设置 python.autocomplete.addBrackets (默认关闭)决定当 VS Code 补全一个函数名时是否自动补全括号。假如你将值设置为真:

  "python.autoComplete.addBrackets": true,

import os 之后 os.getc 自动补全,选择 os.getcwd 它会变成 os.getcwd()。我测试了一下这个功能我虽然设置为真,但是仍然不能补全。

故障排查

如果自动补全和智能感知在一个指定的模块下不能工作,检查下面的原因:

Cause Solution
The path to the python interpreter is incorrect Check the pythonPath setting. Restart VS Code if you make a correction.
The custom module is located in a non-standard location (not installed using pip). Add the location to the python.autoComplete.extraPaths setting and restart VS Code.
VS Code was not launched from the active virtual environment that would set the path to custom modules. Launch VS Code from a command prompt with the correct virtual environment activated, for example: (venv) ter@minal:~$ code.
原因 解决方法
python 解释器的路径错误 检查 pythonPath 设置。在改正错误后重启 VS Code
制定模块在非标准位置(没有使用 pip 安装) 添加位置到 python.autoComplete.extraPaths 设置中,然后重启 VS Code
VS Code 没有从设置制定模块的激活的虚拟环境中启动 从正确的激活的虚拟环境中使用命令提示符启动 VS Code ,例如: (venv) ter@minal:~$ code

在终端(REPL)运行选择部分/行代码

  1. 选择部分代码或者光标停顿的行,按快捷键 shift + enter 运行代码。其次选择后右键运行。
  2. 或者右键打开终端运行,或者 ctrl + shift + p 输入 start REPL 在 REPL环境中运行代码。

格式化

python 拓展支持通过 autopep8(默认),black,yapf 将源代码格式化。

一般格式化设置

设置
(python.formatting.)
默认值 描述
提供者 "autopep8" 指定使用什么格式化工具, “autopep8”, “yapf”, 或 “black”.

格式化器的特别设置

以下的设置应用于单独的格式化器。Python 应用拓展会查找当前 pythonPath 的格式化器。如果要使用另一个位置的格式化器,需要特别设置当前路径。

Formatter Install steps Arguments setting
(python.formatting.)
Custom path setting
(python.formatting.)
autopep8 pip install pep8
pip install –upgrade autopep8
autopep8Args autopep8Path
black pip install black blackArgs blackPath
yapf pip install yapf yapfArgs yapfPath

形如下例自定义参数:

"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"],
"python.formatting.yapfArgs": ["--style", "{based_on_style: chromium, indent_width: 20}"]
"python.formatting.blackArgs": ["--line-length", "100"]

故障排查

如果格式化失败,检查下面这些可能的原因:

Cause Solution
The path to the python interpreter is incorrect Check the pythonPath setting.
The formatter is not installed in the current environment Open a command prompt, navigate to the location specified in the pythonPath setting, and run pip install for the formatter.
The path to the formatter is incorrect. Check the value of the appropriate python.formatting.<formatter>Path setting.
Custom arguments for the formatter are incorrect. Check that the appropriate python.formatting.<formatter>Path setting does not contain arguments, and that python.formatting.<formatter>Args contains a list of individual top-level argument elements such as "python.formatting.yapfArgs": ["--style", "{based_on_style: chromium, indent_width: 20}"].

When using the black formatter, VS Code issues the following warning when pasting source code into the editor: Black does not support the “Format Select” command.

To prevent this warning, add the following entry to your user or workspace settings to disable format on paste for Python files:

"[python]": {
    "editor.formatOnPaste": false
}

重构

Python 拓展有下面的重构命令: Extract Variable, Extract Method, 和 Sort Imports.这三个功能都非常赞。

提取变量

提取全部当前域被选择的文本中出现的相似变量,并且用一个变量替换。

有以下几种调用方法:

  • Context Menu: right-click a selection and select Extract Variable.
  • Command Palette (kb(workbench.action.showCommands)), then Python Refactor: Extract Variable.
  • Assign a keyboard shortcut to the python.refactorExtractVariable command.

refactorExtractVar!

提取方法

Invoked by:

  • Context Menu: right-click a selection and select Extract Method.
  • Command Palette (kb(workbench.action.showCommands)), then Python Refactor: Extract Method.
  • Assign a keyboard shortcut to the python.refactorExtractMethod command.

refactorExtractMethod!

导入排序

导入排序使用 isort 包加强导入功能,使得它按字母表排序,并且也对同一个包中导入多个模块进行排序。

Invoked by:

  • Right-click in editor and select Sort Imports (no selection is required)
  • Command Palette (kb(workbench.action.showCommands)), then Python Refactor: Sort Imports
  • Assign a keyboard shortcut to the python.sortImports command
  • Saving a file when sort on save is enabled.

sortImports

可以设置python.sortImports.args 值对具体参数进行定制。

"python.sortImports.args": ["-rc", "--atomic"],

To use a custom isort script, use the python.sortImports.path setting to specify the path:

Further configurations can be stored in an .isort.cfg file as documented on Configuring isort.

保存时自动对导入自行排序

To automatically sort imports whenever you save a file, add the following entry to your user or workspace settings:

"[python]": {
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37639589/article/details/82529987
今日推荐