goerz / better-apidoc

A version of sphinx-apidoc with support for templating
BSD 2-Clause "Simplified" License
32 stars 6 forks source link

UnboundLocalError: local variable 'package_ns' referenced before assignment #9

Open gbroques opened 6 years ago

gbroques commented 6 years ago

VERSION: better-apidoc 0.1.4

Whenever I run:

better-apidoc -t ./_templates -f ../song_match -o song_match

I get the following error:

WARNING: failed to import 'song_match': No module named 'song_match'
Traceback (most recent call last):
  File "/home/g/anaconda3/bin/better-apidoc", line 11, in <module>
    sys.exit(main())
  File "/home/g/anaconda3/lib/python3.6/site-packages/better_apidoc.py", line 604, in main
    modules = recurse_tree(rootpath, excludes, opts)
  File "/home/g/anaconda3/lib/python3.6/site-packages/better_apidoc.py", line 462, in recurse_tree
    py_files, opts, subs, is_namespace)
  File "/home/g/anaconda3/lib/python3.6/site-packages/better_apidoc.py", line 365, in create_package_file
    text = template.render(**package_ns)
UnboundLocalError: local variable 'package_ns' referenced before assignment

Not sure what I'm doing wrong.

I run the command within ./docs. Here's a snippet of my package structure.

.
├── docs
│   └── _templates
│       ├── module.rst
│       └── package.rst
├── song_match
│   ├── config.py

... The rest is omitted

Whenever I try sphinx-apidoc -f ../song_match -o song_match it works fine.

Any help?

thomaslima commented 6 years ago

Same here. Thanks for reporting.

gbroques commented 6 years ago

Did a little digging. The issue is it's failing to import my song_match module. The WARNING it gives is a critical failure. This is probably because the module is not in sys.path.

I have the following in conf.py though:

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('../'))

So I'm still confused. For now, I found a workaround.

Workaround

I added the following to my conf.py, as this is what @goerz does in the QNET conf.py.

# -- Generate API documentation ------------------------------------------------

def run_apidoc(_):
    """Generage API documentation"""
    import better_apidoc
    better_apidoc.main(
        ['better-apidoc', '-t', './_templates', '--force', '--no-toc',
         '--separate', '-o', './song_match', '../song_mtach'])

def setup(app):
    app.connect('builder-inited', run_apidoc)

Works like a charm!