https://code.visualstudio.com/docs/python/editing#_sort-imports
我已经设定
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
Run Code Online (Sandbox Code Playgroud)
在 vscode 中,看起来选择了正确的 python 解释器,但它没有按预期生成排序的导入。我怀疑 vscode 使用了错误的 isort,我怎样才能找出 vscode 到底使用的是哪个 isort?
我喜欢从精心设计的 Python 项目中获取灵感。
最后一个给我启发的是诗歌库。
我从中复制了很多,但这篇文章的主题是black和isort。
两者都配置良好pyproject.toml:
[tool.isort]
profile = "black"
...
known_first_party = "poetry"
[tool.black]
line-length = 88
include = '\.pyi?$'
exclude = '''
/(
...
)/
'''
Run Code Online (Sandbox Code Playgroud)
格式设置如下Makefile:
format: clean
@poetry run black poetry/ tests/
Run Code Online (Sandbox Code Playgroud)
我认为运行make format会运行black,它将在内部运行isort,但是当我运行时isort .,它随后正确地格式化了导入语句。然后好像black没跑isort。
问:内部运行吗?blackisort
我正在尝试使用 isort 自动修复导入订单。当我pre-commit run --files=myfile.py正确运行时,它会更新我的导入(通过在已知的第三方导入和内部包导入之间添加一行)。但是,当我尝试提交此操作时,预提交将导致提交失败,然后删除第三方导入和内部包之间的行。
我尝试known_third_party在setup.cfg和 in中设置设置.isort.cfg,并且得到相同的行为。但是,我怀疑配置本身不是问题,因为当我运行时它显然被正确识别pre-commit run ...。那么为什么pre-commit run使用了正确的配置,但在实际提交时,似乎忽略了它呢?
如何让挂钩尊重实际提交的配置?
配置是:
.pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 21.12b0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)
Run Code Online (Sandbox Code Playgroud)
.isort.cfg
[settings]
known_third_party=django
Run Code Online (Sandbox Code Playgroud)
附加信息:子目录配置可能导致此行为
我尝试过移动配置文件,现在问题似乎已经解决。我的存储库包含多个应用程序的源代码,其中之一是 python django 应用程序。配置文件位于该子目录中。我已将配置文件移至整个项目的根目录,现在这个特定问题已解决。我想知道是否有一种方法可以进行预提交或排序来识别子目录中的配置,但我将把它留给另一个问题。
我想在 Visual Studio Code 中使用自动保存并应用 flake8、mypy、isort 和 black 等常用工具。
Flake8、mypy 和 black 工作正常,但 isort 根本不工作。有办法解决这个问题吗?
我的 settings.json 文件如下所示:
{
"python.terminal.activateEnvInCurrentTerminal": false,
"python.defaultInterpreterPath": "${workspaceFolder}\\.venv\\Scripts\\python.exe",
"python.linting.enabled": true,
"python.linting.lintOnSave": true,
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.flake8Path": "${workspaceFolder}\\.venv\\Scripts\\pflake8.exe",
"python.linting.mypyEnabled": true,
"python.formatting.provider": "black",
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.formatOnSave": true
}
}
Run Code Online (Sandbox Code Playgroud) 我正在努力使用 isort 库,它正在对我的项目中的导入进行排序。
为了避免循环依赖,我需要按以下顺序导入包:
from foo import *
from bar import *
from eggs import *
from spam import *
Run Code Online (Sandbox Code Playgroud)
但它并没有像您所期望的那样按字母顺序对它们进行排序。
from bar import *
from eggs import *
from foo import *
from spam import *
Run Code Online (Sandbox Code Playgroud)
我尝试使用noqa一些代码来导入行和整个文件,但没有帮助。
如何忽略/noqa orderign 进行该导入?
我有一系列在本地和 Jenkins 上运行的工具来检查和格式化我的 Python 代码:
我使用pyproject.tomlfile 来配置 black、isort with.isort.cfg和 autoflake 与命令行参数,因为我还没有找到任何支持使用配置文件配置它。
有没有办法配置 isort 和 autoflake pyproject.toml?
我希望所有工具都配置为一个文件。
我正在尝试运行预提交挂钩,但是当它们遇到 isort 挂钩时会失败,该挂钩会引发以下错误:
File "/home/el/.cache/pre-commit/repoffrjhcx0/py_env-python3/lib/python3.10/site-packages/_distutils_hack/__init__.py", line 92, in create_module
return importlib.import_module('setuptools._distutils')
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'setuptools'
Run Code Online (Sandbox Code Playgroud)
我正在使用 docker,并且我已经检查了 setuptools 是否已安装在我的全局计算机和 docker 上。我不明白为什么会出现这个错误。我认为 isort 设置了它自己的环境,但是为什么它不会被安装,因为它是在配置文件pyproject.toml中定义的。
以下是我的预提交和 isort 配置:
.pre-commit-config.yaml
repos:
- repo: https://github.com/pycqa/isort
rev: 5.8.0
hooks:
- id: isort
args: ["--multi-line=5", "--line-length=120", "--use-parentheses", "--filter-files"]
exclude: "migrations"
stages: [commit]
Run Code Online (Sandbox Code Playgroud)
毒物文件
[isort]
line_length=120
skip_glob=*migrations*
multi_line_output=5
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
use_parentheses=true
include_trailing_comma=true
lines_between_types=1
lines_after_imports=2
[testenv:isort]
deps =
isort
commands =
isort . --check-only --diff
Run Code Online (Sandbox Code Playgroud)
系统上的Python版本:3.10.1 …
我正在尝试将isort设置为PyCharm中的外部工具.我无法设置过滤器,以便文件路径是链接.
isort的输出是:
ERROR: C:\dev\path\to\a\project\file.py Imports are incorrectly sorted.
Run Code Online (Sandbox Code Playgroud)
根据文档投入 $FILE_PATH$应该是足够的但它不适合我的工作.我已经尝试了几种正则表达式样式而没有任何成功.
我想通过ALE插件设置isort在 Vim 中使用。我已将此快捷方式添加到我的:.vimrc
nnoremap <leader>I :ALEFix isort<CR>
Run Code Online (Sandbox Code Playgroud)
但是,当我激活它时,什么也没有发生。我已经isort在全局和 virtualenv 中安装了。谁能给我提示如何调试/解决这个问题?
目的:使用ST3插件规范python包导入顺序
官网查询有一个“isort”插件:https ://packagecontrol.io/packages/isort
但ST3中没有说明如何使用,安装后也找不到对应的执行入口。
问题:1、有相应的解决方案吗?2.还有其他插件可以对包的导入顺序进行排序吗?
isort ×10
python ×9
pre-commit ×2
python-3.x ×2
autoflake ×1
autosave ×1
git ×1
pycharm ×1
python-black ×1
sublimetext3 ×1
vim ×1
vim-ale ×1
virtualenv ×1