Use of pre-commit with PyCharm
Asked Answered
D

0

13

I am trying to launch my pre-commit hooks from "commit" button in PyCharm (v. 2020.2). I use a conda venv (created with conda create -n py38 python=3.8) where I installed modules with pip install.

My .pre-commit-config.yaml reads:

repos:
- repo: local
  hooks:
  - id: black
    name: black
    language: system
    entry: black --check
    types: [python]

  - id: isort
    name: isort
    language: system
    entry: isort --check-only
    types: [python]

I use local repo here because I will push my code to an intranet repository not connected to the internet.

Running pre-commit run --all-files runs fine from the command line on my local machine. But when I try to commit from PyCharm (enter image description here), it raises the following error:

Traceback (most recent call last):
  File "c:\bib\envs\py38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None, File "c:\bib\envs\py38\lib\runpy.py", line 86, in _run_code exec(code, run_globals)
  File "c:\bib\envs\py38\lib\site-packages\pre_commit\__main__.py", line 1, in   
    from pre_commit.main import main
  File "c:\bib\envs\py38\lib\site-packages\pre_commit\main.py", line 13, in   
    from pre_commit.commands.autoupdate import autoupdate
  File "c:\bib\envs\py38\lib\site-packages\pre_commit\commands\autoupdate.py", line 14, in   
    from pre_commit.clientlib import InvalidManifestError
  File "c:\bib\envs\py38\lib\site-packages\pre_commit\clientlib.py", line 16, in   
    from pre_commit.error_handler import FatalError
  File "c:\bib\envs\py38\lib\site-packages\pre_commit\error_handler.py", line 10, in   
    from pre_commit.store import Store
  File "c:\bib\envs\py38\lib\site-packages\pre_commit\store.py", line 4, in   
    import sqlite3
  File "c:\bib\envs\py38\lib\sqlite3\__init__.py", line 23, in   
    from sqlite3.dbapi2 import *
  File "c:\bib\envs\py38\lib\sqlite3\dbapi2.py", line 27, in   
    from _sqlite3 import * 
ImportError: DLL load failed while importing _sqlite3: The specified module could not be found.

I think that this bug report on pre-commit's github is of particular relevance. If I understand well, it hints that PyCharm is not actually loading the venv and hence not finding installed packages in this environment. No solution is proposed though.

Anyone has a solution for that?

Diphosgene answered 31/8, 2020 at 1:7 Comment(5)
What folder are you in in your PyCharm terminal? Sometimes that can get ya.Coelacanth
Thank you for your comment @zerecees. I am not sure I answering to your question, but let me try. When I open my project, I open the folder containing my .git directory. This folder also contains my .pre-commit-config.yaml file. Except from this I do not specify a working directory anywhere else. When I click on the 'Terminal' tab at the bottom left of the PyCharm window, it opens a prompt in my project directory. This is where I want to be.Diphosgene
(maintainer here) -- this is a bug in conda, I'd suggest using pre-commit from a non-conda environment if you're calling it from an IDE: github.com/conda-forge/pre-commit-feedstock/issues/9Fibrous
I think you can activate conda environment in the terminal and start PyCharm from the same command line instance with PyCharm\bin\pycharm.bat. This way IDE and all processes spawned by it will inherit the environment setup made by conda.Timmytimocracy
Thank you both. The workaround you propose Pavel works indeed. I did not tested the solutions presented in the link shared by @AnthonySottile but I suppose they will work as well. I will test and update.Diphosgene

© 2022 - 2024 — McMap. All rights reserved.