pdoc3 / pdoc

:snake: :arrow_right: :scroll: Auto-generate API documentation for Python projects
https://pdoc3.github.io/pdoc/
GNU Affero General Public License v3.0
1.12k stars 145 forks source link

pre-commit hook usage with virtualenv fails #313

Open zjpiazza opened 3 years ago

zjpiazza commented 3 years ago

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

amaank404 commented 3 years ago

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

Ricocotam commented 3 years ago

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 :

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
kernc commented 3 years ago

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:

Ricocotam commented 3 years ago

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 ?