question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pre-commit doesn't keep current project/virtualenv in sys.path

See original GitHub issue

I’m trying to get Prospector ( https://github.com/landscapeio/prospector ) to run as a pre-commit hook. By default, Prospector will run pylint, including pylint’s import checker.

I was getting confused, because I kept getting output that looks like:

test_mailing_lists (<projectname>/tests/services/platform/email/test_mailing_lists.py):
    L8:0 None: pylint - F0401
    Unable to import '<projectname>.services.platform.email.mailing_lists'

Even though directly running Prospector in the project (as opposed to via pre-commit) doesn’t raise these errors.

When I print sys.path from inside a Python script run by pre-commit as a hook, I get:

['/Users/gkisel/.pre-commit/repodfiUn9/py_env/bin',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python27.zip',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/plat-darwin',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/plat-mac',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/Extras/lib/python',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/lib-tk',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/lib-old',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/site-packages',
 '/Users/gkisel/.pre-commit/repodfiUn9/py_env/lib/python2.7/site-packages/astroid/brain']

Note that this doesn’t appear to include my project directory or my virtualenv path. I think this is why pylint (within Prospector) is unable to successfully run those import statements.

Is this behavior intentional? Am I doing something incorrectly? Is there a way I can get pre-commit to preserve my sys.path when running Python hooks?

For reference, see https://github.com/guykisel/prospector-mirror

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:24 (11 by maintainers)

github_iconTop GitHub Comments

9reactions
scopcommented, Jan 13, 2020

Another tool that is affected by this problem is mypy, it really needs stuff from the project’s venv to work properly unless one pretty much completely ignores errors resulting from imports.

I think the system workaround isn’t really applicable for cases where for example a git GUI tool (such as GitHub Desktop) is launched and stuff committed to a pre-commit enabled working dir; there’s likely no opportunity to activate the project venv there.

Maybe a script approach that would activate the venv would work, dunno. But a problem there is that the way people use venvs differ; some prefer plain python -m venvs, others pyenv virtualenvs, the dirs where the venvs are set up vary, so the script would not be able to activate the venv properly in all cases, and forcing people to use virtualenvs some specific way would be kind of meh.

So it would be great if pre-commit would offer some more assistance in coping with these scenarios. Don’t know what exactly would that be. Maybe e.g. some option that would “record” the currently active virtualenv settings at hook install time and use them later to construct the same environment when running the installed hook, dunno.

1reaction
mirycommented, Nov 21, 2019

Just in case:

I had next hook:

  - repo: local
    hooks:
      - id: pytest
        name: pytest
        entry: poetry run pytest --
        language: python
        types: [python]
        files: ^tests\/.*test_.*\.py$
        pass_filenames: false  # or use with: require_serial

I use poetry. And on each changes of tests, it used different python.

Base on comment @asottile , I changed language: python to language: system and everything started works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python 3.x - pre-commit not using virtual environment
I'm trying to have pre-commit check if my Python project's requirements.txt matches pip freeze (for my current venv) - is there a way...
Read more >
Pycharm does not use virtualenv pep8 for git-hooks : PY-12988
I have pre-commit hook when i commit using Pycharm - it not using PATH from virtualenv (default project interpreter) and use system pep8...
Read more >
pipenv Documentation - Read the Docs
By default, the virtualenv is stored globally with the name of the project's root directory plus the hash of the full path to...
Read more >
Virtual environments for absolute beginners — what is it and ...
In this article I'll try to convince that using a venv (virtual environment) is the way to keep dependencies separate from other projects....
Read more >
virtualenv 1.10.1 - PyPI
Prior to version 1.9, the pip included in virtualenv did not not download ... Items in your environment will show up first on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found