python - 使用 tox 错误排序导入

标签 python django-rest-framework pep8 tox

我正在尝试为 django-rest-framework 做出贡献,运行 isort 后身份验证文件中的导入如下(我添加了导入六):

from __future__ import unicode_literals

import base64

import six
from django.contrib.auth import authenticate, get_user_model
from django.middleware.csrf import CsrfViewMiddleware
from django.utils.translation import ugettext_lazy as _

from rest_framework import HTTP_HEADER_ENCODING, exceptions

当我运行./runtests --lintonly时,它通过了所有测试,但是当我运行tox时,它给了我这个错误:

py27-lint runtests: commands[0] | ./runtests.py --lintonly
Running flake8 code linting
flake8 passed
Running isort code checking
ERROR: /home/nitesh/open_source/django-rest-framework/rest_framework/authentication.py Imports are incorrectly sorted.
isort failed: Some modules have incorrectly ordered imports. Fix by running `isort --recursive .`
ERROR: InvocationError: '/home/nitesh/open_source/django-rest-framework/runtests.py --lintonly'

最佳答案

我遇到了类似的错误(导入排序不正确)。

isort 直接运行时很高兴,但通过 tox 运行时失败。 isort 提示的行是:

import pytest

from my_module import MyThing

isort 在直接运行时知道我的模块 my_module 是第一方(我自己的)代码,而通过 tox 则不然。 t。因此,当直接运行时,它对 pytest 导入和我的导入之间的空行感到满意,但通过 tox 它不想看到空行,因为 pytestmy_module 被解释为第三方导入。

解决方案是将这一行添加到我的setup.cfg中:

[isort]
...
known_first_party = my_module

关于python - 使用 tox 错误排序导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36385671/

相关文章:

python - 将 Mxnet Rec 馈送到 Tensorflow

python - 如何判断通过管道的数据何时结束

Python 3.5卸载Windows 7

python - PEP 8,为什么在关键字参数或默认参数值中 '=' 周围没有空格?

python-3.x - os.system 的多行命令

python - Keras SimpleRNN 混淆

django - 如何让自定义分页类适用于 django Rest Framework v3.6

python - 如何在django中制作像google表单一样的表单?用户可以在哪里根据需要添加或删除字段?用户最多可以添加10-20个字段?

Django ORM 返回陈旧数据,可能存在竞争条件

google-colaboratory - 用于 google colab 的代码格式化程序,如 nb_black