AnswerDotAI / AnswerDotAI/nbdev

`black` is not found when `nbdev_export` is run in pre-commit hook

Offen
#1,366 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
Jupyter Notebook
Sterne
5.3k
Forks
514
Ø Merge
2 T. 30 Min.
Gemergte PRs (30 T.)
8

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit nbdev/export.py, insbesondere black_format, und vergleiche die normale Umgebung mit der in der Traceback beschriebenen pre-commit-Umgebung. Überprüfe die pre-commit-Einrichtung in der verlinkten Dokumentation und reproduziere den Fehler mit nbdev_clean und nbdev_export. Erledigt ist die Aufgabe, wenn nbdev_export mit black_formatting=True als pre-commit-Hook ausgeführt wird und erfolgreich abgeschlossen wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
jupyter, python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.