Gaurav Dadhania
Gaurav Dadhania

Reputation: 5327

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!

Upvotes: 7

Views: 5744

Answers (3)

Tzach
Tzach

Reputation: 13376

Adding to Koterpillar's great answer, you can also configure your pre-commit hook to change the current directory to my_django_project and run pylint from there.

Upvotes: 0

Koterpillar
Koterpillar

Reputation: 8104

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, e.g.

pylint --rcfile=pylint.conf my_django_project

Upvotes: 4

juliomalegria
juliomalegria

Reputation: 24921

Maybe this doesn't fully answer your question, but I suggest to use django-lint, to avoid warnings like:

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

Upvotes: 0

Related Questions