fastai / nbdev

Create delightful software with Jupyter Notebooks
https://nbdev.fast.ai/
Apache License 2.0
4.8k stars 484 forks source link

UnboundLocalError: cannot access local variable '_TEMPLATE' where it is not associated with a value #1412

Closed stepetal closed 2 months ago

stepetal commented 2 months ago

Command to reproduce the error

Error occured at the moment of execution 'nbdev_new' command (https://nbdev.fast.ai/tutorials/tutorial.html#initialise-your-repo-with-nbdev) inside python venv with installed nbdev library.

Detail description

Full error message looks like Traceback (most recent call last): File "/home/alex/PythonProjects/nbdev_env/bin/nbdev_new", line 8, in <module> sys.exit(nbdev_new()) ^^^^^^^^^^^ File "/home/alex/PythonProjects/nbdev_env/lib/python3.12/site-packages/fastcore/script.py", line 119, in _f return tfunc(**merge(args, args_from_prog(func, xtra))) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/alex/PythonProjects/nbdev_env/lib/python3.12/site-packages/nbdev/cli.py", line 91, in nbdev_new _TEMPLATE = f'{_ORG_OR_USR}/{_TEMPLATE}' ^^^^^^^^^ UnboundLocalError: cannot access local variable '_TEMPLATE' where it is not associated with a value

error_displayed_in_cmd

OS and Python versions

OS: Ubuntu 24.04 LTS Python version: Python 3.12.3 nbdev version: nbdev 2.3.19

nbdev library was installed with help of pip: pip 24.0 from /home/alex/PythonProjects/nbdev_env/lib/python3.12/site-packages/pip (python 3.12)

Possible reason of the error

It is possible that the source of error is the incorrect argument to f - string. https://github.com/fastai/nbdev/blob/master/nbs/api/13_cli.ipynb, function _nbdevnew.

_TEMPLATE = f'{_ORG_OR_USR}/{_TEMPLATE}' In this statement variable _TEMPLATE is used before assignment

Probably this is misprint and '_REPOSITORY' should be used instead of '_TEMPLATE'

How to fix locally

nbdev_new - initial command when creating project with nbdev, so it is not possible to use functionality of the library without it. Steps to fix locally:

  1. Create python venv and activate it.
  2. Install nbdev and jupyter lab according to the tutorial mentioned above.
  3. Get the source https://github.com/fastai/nbdev.git and go to created directory.
  4. Replace '_TEMPLATE' with '_REPOSITORY' inside f'{_ORG_OR_USR}/{_TEMPLATE}' in the function _nbdevnew
  5. Execute command 'nbdev_export' to update exported py - files
  6. Uninstall old nbdev library ( pip uninstall nbdev)
  7. Install new nbdev library: pip install .
  8. Go to project directory and execute nbdev_new

nbdev_new_function install_new_nbdev results_of_local_fixing

deven367 commented 2 months ago

Hi @stepetal, would you like to create a PR for this?

stepetal commented 2 months ago

Hi! Thanks @deven367. Yes, I would. I will include changes mentioned in the issue