Skip to content

Unable to determine how to use pre-existing virtualenv for a hook #266

Closed

Description

@EliRibble

Sorry, I'm not sure if an issue is the right place to put this, but considering I've spent some time reading source and docs and haven't found the answer it seems fair to consider this a documentation bug.

My use case is this: I have many Python projects I work on. Each one has its own virtualenv where I install the dependencies. I'd like to use pre-commit to run pylint. Pylint needs to run out of the same virtualenv as the project so that it can import the dependencies for its static analysis. When running the pylint hook from https://github.com/pre-commit/mirrors-pylint it appears that the command that is actually run is:

bash -c . '/Users/eliribble/.pre-commit/repoq_elftwa/py_env-default/bin/activate' && xargs -0 -s4000 pylint

Looking at the source and the docs I can't find a way to indicate to the hook that it should use my virtualenv for the project rather than the virtualenv at /Users/eliribble/.pre-commit/repoq_elftwa/py_env-default. Is there a way to do that?

Activity

  1. chriskuehl commented on Sep 29, 2015

    @chriskuehl
    Member

    One option is to add a hook to .pre-commit-config.yaml like:

    -   repo: local
        hooks:
        -   id: system
            name: PyLint
            entry: python -m pylint.__main__
            language: system
            files: \.py$

    (stolen from https://github.com/Yelp/pgctl)

    Does that work for you? If so, it's probably worth documenting somewhere.

    I don't think there's support for running Python-type hooks in the current environment.

  2. asottile commented on Sep 29, 2015

    @asottile
    Member

    Yeah we're still missing proper documentation from #226

    We'll track that work here: pre-commit/pre-commit.com#50

    @EliRibble do local hooks satisfy your requirements?

  3. EliRibble commented on Sep 29, 2015

    @EliRibble
    Author

    Yeah, that will work, though I changed it a bit since we already had a bash script which hands across the correct settings file to pylint. Looks like this, for anyone who hits the same thing:

    -   repo: local
        hooks:
        -   id: system
            name: PyLint
            entry: ./pylint.sh
            language: system
            files: \.py$
    

    Thanks for the fast response.

  4. asottile commented on Sep 30, 2015

    @asottile
    Member

    No problem! Hopefully I find some time to get those docs updated :)

  5. asottile commented on May 4, 2016

    @asottile
    Member

    local hooks are now documented here: http://pre-commit.com/#repository-local-hooks

  6. EliRibble commented on May 4, 2016

    @EliRibble
    Author

    Looks really good, thanks

  7. webknjaz commented on Jun 23, 2018

    @webknjaz

    To whom it may concern,

    here's config for pylint I ended up having

    diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
    index adec32c..1a18bc3 100644
    --- a/.pre-commit-config.yaml
    +++ b/.pre-commit-config.yaml
    @@ -18,3 +18,23 @@
       hooks:
       - id: pydocstyle
         exclude: blog/migrations
    +
    +- repo: git://github.com/pre-commit/mirrors-pylint.git
    +  rev: v1.9.1
    +  hooks:
    +  - id: pylint
    +    args:
    +    - --msg-template={path}:L {line:3d}:({symbol}) {msg} (C {column:2d}), ::{msg_id}
    +    - --output-format=colorized
    +    - |-
    +        --init-hook=
    +        import os, platform, sys;
    +        sys.path.insert(
    +          0,
    +          "".join([os.getenv("HOME"), "/virtualenv/python", platform.python_version(), "/site-packages"])
    +          if os.getenv("TRAVIS") else
    +          "{}/test_venv/lib/python3.6/site-packages".format(os.getcwd())
    +        )  # inject virtualenv packages into a module search path; could also go for pylint-venv hook
    +    - --load-plugins=pylint_django
    +    - --good-names=pk,urlpatterns
    +    exclude: blog/migrations/
  8. asottile commented on Jun 23, 2018

    @asottile
    Member

    that seems a bit excessive -- why not configure pylint as a local hook like the documentation suggests?

  9. webknjaz commented on Jun 23, 2018

    @webknjaz

    Does it imply installing it manually?

  10. asottile commented on Jun 23, 2018

    @asottile
    Member

    it does, yes but you're also building a virtualenv manually for pylint's sake too

  11. asottile commented on Jun 23, 2018

    @asottile
    Member

    and again, pylint is a bit of a special snowflake here -- it does both static analysis and dynamic analysis (which needs to actually import your code)

  12. webknjaz commented on Jun 24, 2018

    @webknjaz

    Yeah, I just didn't want to mutate existing app's env.

  13. added a commit that references this issue on Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions