标签: autopep8

错误:产生pycodestyle ENOENT

安装了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)

atom-editor autopep8

12
推荐指数
1
解决办法
1735
查看次数

如何在 Visual Studio Code 中格式化 python 代码?

用AutoPep8开了票,但是当我安装autopep8扩展时,似乎没有找到该模块。当我更改配置以告诉它它在哪里时,格式不会执行任何操作。但是,在终端中进行格式化却可以。有任何想法吗?https://github.com/hhatto/autopep8/issues/349#issuecomment-335662565

python formatting autopep8 visual-studio-code

12
推荐指数
2
解决办法
5万
查看次数

VS Code Python autopep8 不支持 2 个空格悬挂缩进

我试图让 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 格式化程序是否可以设置此属性?

python autopep8 visual-studio-code

12
推荐指数
4
解决办法
8801
查看次数

如何让pylint、flake8或pycodestyle自动更正错误?

我已在 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)

但是,代码中仍然存在相同的样式错误。如何使它们自动更正?

pylint flake8 autopep8 pycodestyle

11
推荐指数
2
解决办法
8828
查看次数

Auto-PEP8 通过将我的 lambda 转换为 def 函数来添加行,如何禁用这种特定的自动格式?

我正在使用 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 的事情。

python pep8 autopep8

10
推荐指数
3
解决办法
2100
查看次数

在 VS Code python 笔记本中将 autopep8 和 linting 添加到 jupyter

问题

错误突出显示和自动格式化可以成为帮助人们创建出色笔记本的绝佳工具。我正在尝试更改 VS 代码上的设置,以允许我在 python 笔记本中自动格式化为 pep8。

此 Jupiter 笔记本页面上,我发现我必须在 VSCode 的设置 > 首选项中的 .json 文件中添加一些行才能执行此操作。我特别有兴趣将我的代码更改为 pep8 编码约定,并添加 linting 以突出显示错误。

  1. linting(错误突出显示)
  2. 自动格式化(autopep8)

我在 Ubuntu 18.04 上使用 VS Code。以下是我的尝试,导致错误“不支持或定义代码语言”。

试图

在 VS code 中安装 Python 扩展和 autopep8 扩展并运行后

pip3 install autopep8
Run Code Online (Sandbox Code Playgroud)

我收到错误消息并且无法使用 pep8。

在此输入图像描述

如果您知道如何在 VS Code 中为 Jupyter 笔记本设置高效的工作环境,我将非常感谢您的帮助

概括

如何设置:

  1. linting(错误突出显示)
  2. 自动格式化(autopep8)

在 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

9
推荐指数
1
解决办法
8026
查看次数

Visual Studio Code 和 Autopep8 格式化程序

我正在尝试将 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 在控制托盘中不可用。

在此输入图像描述

谢谢道森

python autopep8 visual-studio-code

9
推荐指数
1
解决办法
2万
查看次数

如何避免在 VSCode 中的一行中使用 python autopep8 格式?

我在使用VSCodeautopep8的 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 1.3.4
  • VSCode 1.23.1

python formatting autopep8 visual-studio-code

8
推荐指数
1
解决办法
2601
查看次数

如何让 pylint 和 autopep8 就如何缩进包装的函数定义达成一致?

如何让美化器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)

python pylint autopep8

8
推荐指数
1
解决办法
400
查看次数

vscode autopep8 allow statements before imports

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 …

python autopep8 visual-studio-code vscode-settings

8
推荐指数
2
解决办法
1742
查看次数