Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access python interpreter in VSCode version controll when using pre-commit

I'm using pre-commit for most of my Python projects, and in many of them, I need to use pylint as a local repo. When I want to commit, I always have to activate python venv and then commit; otherwise, I'll get the following error:

black....................................................................Passed
pylint...................................................................Failed
- hook id: pylint
- exit code: 1

Executable `pylint` not found

When I use vscode version control to commit, I get the same error; I searched about the problem and didn't find any solution to avoid the error in VSCode.

This is my typical .pre-commit-config.yaml:

repos:
-   repo: https://github.com/ambv/black
    rev: 21.9b0
    hooks:
    - id: black
      language_version: python3.8
      exclude: admin_web/urls\.py
-   repo: local
    hooks:
    -   id: pylint
        name: pylint
        entry: pylint
        language: python
        types: [python]
        args: 
         - --rcfile=.pylintrc

like image 320
Mehdi Avatar asked Mar 11 '26 02:03

Mehdi


2 Answers

you have ~essentially two options here -- neither are great (language: system is kinda the unsupported escape hatch so it's on you to make those things available on PATH)

you could use a specific path to the virtualenv entry: venv/bin/pylint -- though that will reduce the portability.

or you could start vscode with your virtualenv activated (usually code .) -- this doesn't always work if vscode is already running


disclaimer: I created pre-commit

like image 72
Anthony Sottile Avatar answered Mar 13 '26 16:03

Anthony Sottile


I found a great solution. Use poetry or hatch!

When you use either of these, you can easily access your environment with, e.g., poetry run command. That's the trick to accessing local pylint.

Here is an example using poetry; everything is the same except language and entry:

repos:
    - repo: https://github.com/ambv/black
      rev: 21.9b0
      hooks:
          - id: black
            language_version: python3.8
            exclude: admin_web/urls\.py
    - repo: local
      hooks:
          - id: pylint
            name: pylint
            entry: poetry run pylint # was: pylint
            language: system         # was: python
            types: [python]
            args:
                - --rcfile=.pylintrc

And works like a charm in VS Code!

like image 27
Mehdi Avatar answered Mar 13 '26 16:03

Mehdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!