pdoc icon indicating copy to clipboard operation
pdoc copied to clipboard

pre-commit hook usage with virtualenv fails

Open zjpiazza opened this issue 4 years ago • 4 comments

Expected Behavior

After running pre-commit hook, to have generated pdoc output in the directory

Actual Behavior

ModuleNotFoundError is raised when running the pdoc pre-commit hook

Steps to Reproduce

  1. Install pre-commit framework
  2. Add pdoc to .pre-commit-config.yaml hooks
  3. Install pre-commit hooks
  4. Run the command "pre-commit run --all-files"

Additional info

  • pdoc version: 0.9.2

zjpiazza avatar Feb 19 '21 04:02 zjpiazza

please share the .pre-commit-config.yaml file where you have added pdoc hook and possibly the github project link if the project is public

amk200 avatar Feb 25 '21 03:02 amk200

I'm having the same issue, here is my pre-commit (only pdoc related) :

-   repo: https://github.com/pdoc3/pdoc
    rev: master
    hooks:
    -   id: pdoc
        name: pdoc
        description: 'pdoc: Auto-generate API documentation for Python projects'
        entry: pdoc --html -f -o docs module1 module2 module3
        language: python
        language_version: python3
        require_serial: true
        types: [python]
        always_run: true

I have some packages installed that are out of stdlib that raise ModyleNotfoundError. I didn't take time to replicate with minimal version but it should be easy :

  • Create new git project
  • Create virtualenv
  • install pre-commit hook
  • add the hook as described above (a single module is enough, even a single file)
  • pip install (using virtualenv) a module
  • import a non stdlib module (pandas/numpy/whatever) in your project
  • commit to trigger pre-commit hooks

A possible source of success is which python is used to trigger pdoc within pre-commit. If it's systems default (python or python3), depending on whether the module is installed or not, it might work.

Edit : I looked into my logs and it seems that pre-commit has its own virtualenv (kinda). It's probably more related to pre-commit than pdoc. Pdoc triggers this because it imports modules (other hooks usually don't, they only take them as text).

/Users/<username>/.cache/pre-commit/<repo_version>/py_env-python3/lib/python3.8/site-packages/pdoc/__init__.py", line 224, in import_module

Ricocotam avatar Mar 22 '21 16:03 Ricocotam

I looked into my logs and it seems that pre-commit has its own virtualenv

Seems to me the only workaround, then, is to use repository-local hooks? Something like:

-   repo: local
    hooks:
    -   id: pdoc
        name: pdoc
        language: system
        entry: python -m pip install pdoc3 && pdoc --html -f -o docs package

:confused:

kernc avatar Mar 22 '21 23:03 kernc

It does work and I agree with you, it's probably the only solution. Do you want a PR to include this in the readme ?

Ricocotam avatar Mar 27 '21 13:03 Ricocotam