Configuring Pylint for Django projects

G'Day,

I have a number of Django projects and a number of other Python projects as git repositories. I have pre-commit hook that runs Pylint on my code before allowing me to commit it - this hook doesn't know whether the project is a Django application or a vanilla Python project.

For all my Django projects, I have a structure like:

> my_django_project
|-- manage.py
|-- settings.py
|--> apps
   |--> my_django_app
      |-- models.py
      |-- admin.py 

Now, when I run pylint on this project, it gives me errors like:

F:  4,0: Unable to import 'my_django_app.models'

for my_django_app.admin module for example.

How to do I configure Pylint, so that when it is going over my django projects (not vanilla python projects), it knows that the my_django_project/apps should also be in the sys.path ? Normally, manage.py adds it to the sys.path .

Thanks!


Take a look at init_hook in pylint configuration file.

init-hook=import sys; sys.path.insert(0, 'my_django_project/apps');

You will obviously need a configuration file per Django application, and run pylint as, eg

pylint --rcfile=pylint.conf my_django_project

也许这并不能完全回答你的问题,但我建议使用django-lint,以避免警告:

F:  4: Unable to import 'myapp.views'
E: 15: MyClass.my_function: Class 'MyClass' has no 'objects' member
E: 77: MyClass.__unicode__: Instance of 'MyClass' has no 'id' member

添加到Koterpillar伟大的答案,您还可以配置您的预提交挂钩将当前目录更改为my_django_project并从那里运行pylint。

链接地址: http://www.djcxy.com/p/10256.html

上一篇: 使用归档时xcodebuild参数被忽略

下一篇: 为Django项目配置Pylint