I added this to pre-commit-config.yml
- repo: local
hooks:
- id: python-hook
name: python-hook
entry: python python_script.py
language: python
pass_filenames: false
Where python_script.py
has the following content
#!/usr/bin/env python
import os
print(os.environ["VIRTUAL_ENV"])
import requests
The package requests
is installed inside the active virtual environment, when I run pre-commit I get this output
/path/to/home/.cache/pre-commit/repouecs3sp4/py_env-python3.7
Traceback (most recent call last):
File "python_script.py", line 7, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
The issue here is that the path to the virtual environment is apparently switched to a different one /path/to/home/.cache/pre-commit/repouecs3sp4/py_env-python3.7
.
Is there something I'm missing here? Thanks for your help
kind of the point of pre-commit is it installs tools in isolated environments so they don't interfere with whatever state your current development environment is in -- language: python
tells pre-commit to create its own python
virtualenv
if you don't want that you can use language: system
-- but this is the unsupported escape hatch for when you don't want managed tools (and note that pre-commit won't provision those environments for your contributors so they will need to make sure they have the particular environment set up and activated)
the better way is to declare the dependencies your local
hook needs to pre-commit such that it can provision an environment for you using additional_dependencies
-- for example if you needed packaging
in your tool:
repos:
- repo: local
hooks:
- id: python-hook
name: python-hook
entry: python python_script.py
language: python
pass_filenames: false
additional_dependencies: [packaging]
note again that pre-commit does not install from the repository under test (if it did, caching would be difficult-to-intractable) so you cannot use things like -r requirements.txt
in additional_dependencies
)
disclaimer: I created pre-commit