上一篇将 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 |
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. setting. |
Custom arguments for the formatter are incorrect. | Check that the appropriate python.formatting. setting does not contain arguments, and that python.formatting. 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.这三个功能都非常赞。
提取全部当前域被选择的文本中出现的相似变量,并且用一个变量替换。
有以下几种调用方法:
kb(workbench.action.showCommands)
), then Python Refactor: Extract Variable.python.refactorExtractVariable
command.!
Invoked by:
kb(workbench.action.showCommands)
), then Python Refactor: Extract Method.python.refactorExtractMethod
command.!
导入排序使用 isort 包加强导入功能,使得它按字母表排序,并且也对同一个包中导入多个模块进行排序。
Invoked by:
kb(workbench.action.showCommands)
), then Python Refactor: Sort Importspython.sortImports
command可以设置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
}
}