Skip to content

Pre-commit hook imports fail with pyenv venv. #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Spenhouet opened this issue Apr 19, 2024 · 2 comments
Open

Pre-commit hook imports fail with pyenv venv. #265

Spenhouet opened this issue Apr 19, 2024 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@Spenhouet
Copy link

We are using pyenv to manage our virtual environments.
I was unable to get the pre-commit hook working in a way that it could be added to our repositories. It always fails to resolve imports.

The docs directly address this here: https://github.com/RobertCraigie/pyright-python?tab=readme-ov-file#pre-commit
There one is instructed to set the venvPath and venv settings. We do not have a pytoml in our projects so I tried to configure this using the pyrightconfig.json.

{
    "venvPath": "~/.pyenv/versions",
    "venv": "myproject"
}

This fails in constructing the venv path and concatenates the current dir with the venvPath /home/spe/myproject/~/.pyenv/versions.
Entering a absolute path works, but then I can't commit this to our repos.
Our team does have the envs all in the same place, as that is how pyenv puts them. So it would be great if the relative home dir path would work.

When I execute pyright via CLI I can simply provide the --pythonpath argument (https://github.com/microsoft/pyright/blob/main/docs/command-line.md),
The JSON config does not have this option (https://github.com/microsoft/pyright/blob/main/docs/configuration.md#main-configuration-options).

Since the pre-commit plugin runs the CLI command, I tried adding the --pythonpath as args to the pre-commit hook definition (https://pre-commit.com/#hooks-args), but this seems to be ignored or not passed on.

I then switched to a local hook definition based on https://github.com/RobertCraigie/pyright-python/blob/main/.pre-commit-hooks.yaml.
This doesn't work as it uses the python language, which as stated by the maintainer of pre-commit, creates an isolated virtual env for the python execution (https://stackoverflow.com/a/70780205/2230045), always missing the imports.
I therefore switched to the system language, which didn't work either when the hook is executed through the VS Code source control commit dialog, as VS code does not seem to make use of the currently active venv for these executions.

What finally worked:

.pre-commit-config.yaml

repos:
  - repo: local
    hooks:
      - id: pyright
        name: pyright
        description: "Python command line wrapper for pyright, a static type checker"
        entry: bin/lint-pyright.sh
        language: script
        "types_or": [python, pyi]
        require_serial: true
        additional_dependencies: []
        minimum_pre_commit_version: "2.9.2"

bin/lint-pyright.sh

#!/bin/bash
VENV_NAME="myproject"

eval "$(pyenv init -)" # initialize pyenv for current shell
eval "$(pyenv virtualenv-init -)" # initialize pyenv-virtualenv for current shell

pyenv shell $VENV_NAME

python -m pyright "$@"

requirements-dev.txt

pyright==1.1.359

Maybe this helps someone search for a solution.
But more importantly, maybe something could be improved:

  • Properly handle the home dir shortcut ~ in venvPath in pyrightconfig.json
  • Accept pythonpath as args for the pre-commit hook
  • Provide pythonPath in pyrightconfig.json
  • Directly accept the venvPath and venv arguments for the pre-commit hook
  • Provide a more direct way to configure the pre-commit hook to run on a virtual env.
@cmclaughlin
Copy link

@Spenhouet thank you very much for posting your solution!

@RobertCraigie RobertCraigie added the help wanted Extra attention is needed label May 3, 2025
@RobertCraigie
Copy link
Owner

Thanks for the report and sorry for the delayed response.

  • Properly handle the home dir shortcut ~ in venvPath in pyrightconfig.json
  • Provide pythonPath in pyrightconfig.json

These are feature requests for the main pyright package, pyright-python does not handle the pyrightconfig.json file itself. If you'd still like these to be supported I'd recommend asking in the https://github.com/microsoft/pyright repo.

Accept pythonpath as args for the pre-commit hook
Directly accept the venvPath and venv arguments for the pre-commit hook
Provide a more direct way to configure the pre-commit hook to run on a virtual env.

I don't use pre-commit anymore so I'm not interested in supporting this myself but I'd be happy to accept a PR adding these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants