19

My project structure looks like this:

project/
   app/
      main.py
   venv/
   .pylintrc
   .pre-commit-config.yaml

When I am trying to edit project/app/main.py and it fails with

Unable to import 'psycopg2' (import-error)

But when I am trying to pylint this file directly, it works.

.pre-commit-config.yaml looks like this:

-   repo: https://github.com/PyCQA/pylint
    rev: pylint-2.4.2
    hooks:
    -   id: pylint
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
palkan
  • 321
  • 2
  • 5

2 Answers2

29

not sure if this made it into pylint proper but there's a disclaimer on the bottom of pre-commit/mirrors-pylint

pre-commit runs pylint from an isolated virtualenv. Many of pylint's checks perform dynamic analysis which will fail there. You may find configuring pylint as a local hook more useful.

if you have very few dependencies, additional_dependencies might be enough to make it work, but using local hooks for things which need to (~essentially) import your code is probably your best bet


disclaimer: I'm the author of pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • 1
    The issue applies at least to flake8, pylint and mypy. If one takes these out of the pre-commit set of hooks they will likely ask themselves why to use pre-commit at all. I do not imply they should, just that current design limits its practical use considerably. – sorin Jun 29 '21 at 08:21
  • 1
    flake8 does not need your installed dependencies -- and it's fairly easy to get mypy working by explicitly listing your typed dependencies. I don't use pylint personally so I don't have any experience there – anthony sottile Jun 30 '21 at 11:49
-2

One workaround that I used in the past was to force pre-commit to install current project. Be warned that while this works is not supported by the author of pre-commit in any way, in fact being actively discouraged, mainly because it prevents pre-commit from using immutable caches.

Once the virtualenv is created it will not be updated again and pre-commit does not have any command line option to tell to not trust local cache. If you get into errors your only option is to reset the entire pre-commit disk cache, this likely means removing gigabytes of data from ~/.cache./pre-commit when you run pre-commit clean. Doing that will slowdown running pre-commit on all other projects you have... :(

Ideally the tool should have an option to invalidate only the environments from the current project and not entire cache.

- repo: https://github.com/pre-commit/mirrors-pylint
  rev: v3.0.0a3
  hooks:
  - id: pylint
    additional_dependencies:
    - .  # <-- that makes it install current project
    - flaky
sorin
  • 161,544
  • 178
  • 535
  • 806