Access python interpreter in VSCode version controll when using pre-commit
Question:
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
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
I found a great solution. Use poetry!
When you are using poetry
, you can easily access your environment by using 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 before
language: system # was python before
types: [python]
args:
- --rcfile=.pylintrc
And works like a charm in VScode!
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
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
I found a great solution. Use poetry!
When you are using poetry
, you can easily access your environment by using 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 before
language: system # was python before
types: [python]
args:
- --rcfile=.pylintrc
And works like a charm in VScode!