安装了linter-pep8后出现此错误.
Error: spawn pycodestyle ENOENT
at exports._errnoException (util.js:1026:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Run Code Online (Sandbox Code Playgroud) 用AutoPep8开了票,但是当我安装autopep8扩展时,似乎没有找到该模块。当我更改配置以告诉它它在哪里时,格式不会执行任何操作。但是,在终端中进行格式化却可以。有任何想法吗?https://github.com/hhatto/autopep8/issues/349#issuecomment-335662565
我试图让 autopep8 工作以正确缩进带有 2 个空格而不是 4 个空格的 Python 代码。我正在使用带有 Python 扩展的 VS Code,它使用 autopep8 进行格式化。我在这里发现可以将 autopep8 配置为使用 2 个空格
"python.formatting.autopep8Args": ["--indent-size=2"]
Run Code Online (Sandbox Code Playgroud)
但它对我不起作用。
我的情况是这样的。当我按回车键时,它正确地开始下一行,缩进与上一行相同。在一个左括号后按回车,它会正确地以另外 2 个空格开始新行。但是当我粘贴或保存时(我将“editor.formatOnPaste”和“editor.formatOnSave”设置为true),烦人的事情发生了:括号内的所有2空格缩进都变成了4(其他2空格缩进不受影响) . 为什么要这样做,我怎样才能让它到处都是 2 个空格?
====编辑====
我发现 pylint 错误Wrong hanging indentation (remove 2 spaces). [bad-continuation]
。这是因为我的 pylintrc 有indent-after-paren=2
. 我想知道 autopep8 或其他 Python 格式化程序是否可以设置此属性?
我已在 Ubuntu 18.04 上使用的 VM 上正确安装了上述所有模块。在特定脚本或文件夹上运行它们中的任何一个时,它们都会正确识别样式错误并将其输出到控制台中。例如:
(venv) .../src$ python3.6 -m flake8
./free_prediction.py:8:1: E303 too many blank lines (5)
./free_prediction.py:8:28: E231 missing whitespace after ','
./free_prediction.py:10:5: E225 missing whitespace around operator
./free_prediction.py:12:3: E225 missing whitespace around operator
./free_prediction.py:15:13: E225 missing whitespace around operator
Run Code Online (Sandbox Code Playgroud)
但是,代码中仍然存在相同的样式错误。如何使它们自动更正?
我正在使用 Visual Studio Code 并且 PEP8 会自动格式化我的代码的一部分,我只是在学习 lambdas 并且我有一个像这样的 3 行代码:
它来自这3 行代码:
# Lambda example
divide = lambda x, y: x/y
print(divide(10, 2))
Run Code Online (Sandbox Code Playgroud)
对于这7 行代码:
# Lambda example
def divide(x, y): return x/y
print(divide(10, 2))
Run Code Online (Sandbox Code Playgroud)
有谁知道我如何让这个程序特别不将我的 lambda 函数转换为 def 函数?
它已经很好地格式化了我的代码,所以我不想完全禁用这个自动功能,只是为了 lambda 的事情。
错误突出显示和自动格式化可以成为帮助人们创建出色笔记本的绝佳工具。我正在尝试更改 VS 代码上的设置,以允许我在 python 笔记本中自动格式化为 pep8。
在此 Jupiter 笔记本页面上,我发现我必须在 VSCode 的设置 > 首选项中的 .json 文件中添加一些行才能执行此操作。我特别有兴趣将我的代码更改为 pep8 编码约定,并添加 linting 以突出显示错误。
我在 Ubuntu 18.04 上使用 VS Code。以下是我的尝试,导致错误“不支持或定义代码语言”。
在 VS code 中安装 Python 扩展和 autopep8 扩展并运行后
pip3 install autopep8
Run Code Online (Sandbox Code Playgroud)
我收到错误消息并且无法使用 pep8。
如果您知道如何在 VS Code 中为 Jupyter 笔记本设置高效的工作环境,我将非常感谢您的帮助
如何设置:
在 python 笔记本的 VS 代码中。
编辑1:
我也尝试在命令面板中运行autopep8
并收到错误
Command 'autopep8' resulted in an error (Running the contributed command: 'extension.sayHello' failed.)
Run Code Online (Sandbox Code Playgroud) python visual-studio autopep8 visual-studio-code jupyter-notebook
我正在尝试将 python 格式化程序设置为 autopep8。它已正确安装,但它从未作为 VSCode 中的格式化选项。
"python.formatting.autopep8Path": "C:\\Users\\DawsonSchaffer\\AppData\\Roaming\\Python\\Python39\\Scripts\\autopep8.exe",
"python.formatting.autopep8Args": [
"--max-line-length",
"160",
"--aggressive",
"--aggressive",
"--in-place"
],
"[python]": {
"editor.defaultFormatter": "autopep8",
},
Run Code Online (Sandbox Code Playgroud)
我已经更改了所有设置。但是我不确定要为 autopep8 添加什么。
"[python]": {
"editor.defaultFormatter": "ms-python.python",
},
or
"[python]": {
"editor.defaultFormatter": "autopep8",
},
Run Code Online (Sandbox Code Playgroud)
此外,autopep8 在控制托盘中不可用。
谢谢道森
我在使用VSCode和autopep8的 python 文件中的 var 名称有问题,这是一个例子:
...
formal_education: bool
...
Run Code Online (Sandbox Code Playgroud)
Autopep 格式化程序更改格式如下:
...
formal_education:
bool
...
Run Code Online (Sandbox Code Playgroud)
引发由flake8检测到的 SyntaxError ,因此,我尝试了特殊注释,例如:# autopep8: noqa
和其他使用某些特定 PEP 的注释,但似乎编辑器跳过了之前的某个阶段并且似乎不可能避免这种行为。
我知道,这个问题是关于对变数名称的字内。
现在的解决方案是在保存阶段禁用格式检查
"editor.formatOnSave": false
,但我想知道如何解决这个问题,可能是 autopep 的一些配置或直接通过 VSCode,或者如何直接在文件中应用特殊注释以实现此目标。
谢谢!
我在用:
如何让美化器autopep8
和 linterpylint
就如何缩进我的代码达成一致,而不完全禁用缩进格式/linting?我不介意它是第一种格式还是第二种格式,只要我可以Alt+F
在 VSCode 中点击并信任输出。
美化者希望代码像这样缩进......
# autopep8 prettifier
def sum(
a: int,
b: int
) -> int:
"""Return the sum of a and b."""
return a + b
Run Code Online (Sandbox Code Playgroud)
...但 linter 想要这样。
# pylint linter
def sum(
a: int,
b: int
) -> int:
"""Return the sum of a and b."""
return a + b
Run Code Online (Sandbox Code Playgroud)
PEP8 标准将此列为格式化函数的一种方式,但他们没有提到当右括号放在单独的行上时如何缩进。我真的更喜欢有额外的换行符,因为这将输出格式放在自己的行上,它减少了将空行作为函数体中第一行的冲动。我稍微喜欢上面的第一个选项,因为这个选项将右括号与必须打开一个的行对齐。当使用类型提示时,Google 的 Python 风格指南推荐第一个缩进示例。
# PEP8 standard
def sum(
a: int,
b: int) -> int:
"""Return the sum …
Run Code Online (Sandbox Code Playgroud) I'm using vscode with the python plugin and autopep8 with
"editor.formatOnSave": true
.
I have local packages I need to import, so I have something like
import sys
sys.path.insert(0, '/path/to/packages')
import localpackage
Run Code Online (Sandbox Code Playgroud)
but when I save, vscode/autopep8 moves all import statements before code, so python can't find my local package.
import sys
import localpackage
sys.path.insert(0, '/path/to/packages')
Run Code Online (Sandbox Code Playgroud)
how can I tell vscode/autopep8 that it's okay to put a statement before imports, or is there a more correct way of importing local …
autopep8 ×10
python ×8
formatting ×2
pylint ×2
atom-editor ×1
flake8 ×1
pep8 ×1
pycodestyle ×1