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.
How can I solve this?
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
Try this:
- repo: local
hooks:
- id: python-hook
name: python-hook
entry: .venv/bin/python python_script.py
pass_filenames: false
language: system
I use it as follows:
- repo: local
hooks:
- id: mypy
name: mypy
entry: .venv/bin/mypy src
pass_filenames: false
language: system
- id: basedpyright
name: basedpyright
entry: .venv/bin/basedpyright src
pass_filenames: false
language: system
- id: pytest
name: pytest
entry: .venv/bin/pytest
pass_filenames: false
language: system
The tools need project dependencies, which are nicely listed in the lock file.
The additional_dependencies config key does not support the lock file, nor pyproject.toml, nor any other requirements.txt
I think pre-commit will install the latest dependency version with that key, but I don't need the latest version, I need to check/test the source with actual dependencies from the lock file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With