pre-commit doesn't keep current project/virtualenv in sys.path
See original GitHub issueI’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:
- Created 9 years ago
- Comments:24 (11 by maintainers)
Top GitHub Comments
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.
Just in case:
I had next hook:
I use
poetry
. And on each changes of tests, it used different python.Base on comment @asottile , I changed
language: python
tolanguage: system
and everything started works.