我使用黑色来格式化普通.py文件以及 Jupyter Notebook 文件 ( .ipynb)。对于笔记本电脑,我想要一个更短的line-length.
是否可以用黑色为不同的文件扩展名指定不同的格式化规则?
我想对我的一个 python 项目使用“黑色代码格式化程序”。我已经使用“pip install black”成功安装了它,它在cmd中给出了一个输出,表明所有要求都已经得到满足(包括“typed-ast>1.4.0”)。
但是,当我在安装后尝试运行“black --help”时,出现错误:“ImportError:无法从“typed_ast”导入名称“_ast3”。有谁知道这里可能是什么问题?
我有以下pyproject.toml配置黑色:
[tool.black]\nexclude = 'foo.py'\n如果我black .从仅包含 的项目根文件夹运行foo.py,我会得到No Python files are present to be formatted. Nothing to do \xef\xbf\xbd预期的结果。
但是,当我foo.py从 VS Code 中保存时(我将黑色配置为格式化程序并启用了保存时格式化),文件仍采用黑色格式。
有趣的是,VS Code 似乎也支持其他配置,例如line-length.
有没有办法让 VSCode 遵循配置exclude?
python code-formatting visual-studio-code pyproject.toml black-code-formatter
最近,我开始在我的项目中使用黑色代码格式化程序,有时很难跟踪我是否格式化了所有更改的文件。假设我的项目包含 20 个不同的脚本(.py 文件),并且我对其中最后 5 个脚本进行了更改,就像我在提交之前通常所做的那样。
第一个选项
black /project_directory
第二个选项
black script1.py
black script2.py
等等。
有没有办法在 git commit 之前自动执行此过程?
预先非常感谢
python code-formatting github automated-refactoring black-code-formatter
是的,我的理解是,black让它采取不同的行为几乎没有余地,但我想知道处理这样的事情的最佳方法(我的原始代码):
@dataclass
class Thing1:
    property1: int                    # The first property.
    property2: typing.List[int]       # This is the second property
                                      # and the comment crosses multiple lines.
现在,当我运行它时black,它给了我:
@dataclass
class Thing1:
    property1: int  # The first property.
    property2: typing.List[int]  # This is the second property
    # and the comment crosses multiple lines.
这并不是我想要的那样。
有没有办法拉黑让评论保持一致?我不关心每个字段从哪一列开始,但最好返回到可读性质,每个字段中的注释都排列整齐:
@dataclass
class Thing1:
    property1: int  # The first property.
    property2: typing.List[int]  # This is the second property
                                 # and the comment crosses …我正在尝试设置格式化程序“黑色”以使用 PyCharm。我尝试使用标准的外部工具首选项和文件观察器插件进行设置。两者都没有为我工作。
我收到这些错误:
我已经安装了它,/Users/Scott/Library/Python/3.8/lib/python/site-packages/black.py并希望它在我保存时为所有 Python 项目文件全局运行。
这是我的偏好的样子:
我是 Python 和 PyCharm 以及 Stack Overflow 的新手,并且仍在设置它。我感谢您的帮助。
application-settings ide-customization pycharm python-3.x black-code-formatter