AnswerDotAI / AnswerDotAI/nbdev
`black` is not found when `nbdev_export` is run in pre-commit hook
- Dominant language
- Jupyter Notebook
- Stars
- 5.3k
- Forks
- 513
- Avg merge
- 2d 30m
- Merged PRs (30d)
- 8
Description
When creating a brand new repository and setting `black_formatting=True` in `settings.ini`, indeed `black` is run as part of `nbdev_export` correctly. However, when I add `nbdev_clean` and `nbdev_export` as pre-commit hooks, exactly as in the [docs](https://nbdev.fast.ai/tutorials/pre_commit.html), `nbdev_clean` runs correctly but `nbdev_export` breaks with the following traceback:
```
Traceback (most recent call last):
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/nbdev/export.py", line 36, in black_format
try: import black
ModuleNotFoundError: No module named 'black'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/bin/nbdev_export", line 8, in
sys.exit(nbdev_export())
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/fastcore/script.py", line 119, in _f
return tfunc(**merge(args, args_from_prog(func, xtra)))
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/nbdev/doclinks.py", line 138, in nbdev_export
for f in files: nb_export(f)
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/nbdev/export.py", line 49, in nb_export
nb.process()
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/nbdev/process.py", line 126, in process
for proc in self.procs: self._proc(proc)
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/nbdev/process.py", line 119, in _proc
for cell in self.nb.cells: self._process_cell(proc, cell)
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/nbdev/process.py", line 110, in _process_cell
if callable(proc) and not _is_direc(proc): cell = opt_set(cell, proc(cell))
File "/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10/lib/python3.10/site-packages/nbdev/export.py", line 37, in black_format
except: raise ImportError("You must install black: `pip install black` if you wish to use black formatting with nbdev")
ImportError: You must install black: `pip install black` if you wish to use black formatting with nbdev
```
I can make things work by activating the environment `/Users/ulupo/.cache/pre-commit/repochwlih_v/py_env-python3.10` and installing `black` there, but this is not an ideal solution.
Contributor guide
Research direction
Start with nbdev/export.py, especially black_format, and compare the normal environment with the pre-commit environment described in the traceback. Review the pre-commit setup in the linked documentation and reproduce the failure with nbdev_clean and nbdev_export. Done means nbdev_export completes with black_formatting=True when run as a pre-commit hook.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- jupyter, python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100