question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to run nbqa on notebooks present in directory without __init__.py

See original GitHub issue

OS: Ubuntu 18.04 nbqa version: 0.1.21

I have a notebook present in a directory which is not a python package. When i try to run the following command nbqa black <my_notebook_file>, I get a StopIteration exception traceback. I looked into the code and I was able to figure out why it is raising this exception.

def _create_blank_init_files(notebook: Path, tmpdirname: str) -> None:
    parts = notebook.resolve().relative_to(Path.cwd()).parts

    for idx in range(1, len(parts)):
        # next on directory with no __init__.py raises StopIteration exception
        init_file = next(Path(os.path.join(*parts[:idx])).glob("__init__.py"))
        if init_file is not None:
            Path(tmpdirname).joinpath(init_file).parent.mkdir(
                parents=True, exist_ok=True
            )
            Path(tmpdirname).joinpath(init_file).touch()

I fixed the code locally and ran isort, black, flake8 and mypy. This is the fix I made locally

import contextlib

def _create_blank_init_files(notebook: Path, tmpdirname: str) -> None:
    parts = notebook.resolve().relative_to(Path.cwd()).parts

    for idx in range(1, len(parts)):
        with contextlib.suppress(StopIteration):
            init_file = next(Path(os.path.join(*parts[:idx])).glob("__init__.py"))
            if init_file is not None:
                Path(tmpdirname).joinpath(init_file).parent.mkdir(parents=True, exist_ok=True)
                Path(tmpdirname).joinpath(init_file).touch()

Are you mandating the presence of __init__.py file in the directory of notebooks to run nbqa?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
girip11commented, Sep 8, 2020

Hey @MarcoGorelli, I am interested in contributing to this library. I use VSCode as IDE. I was going through nbqa library dev ecosystem, setting up its dependencies in VSCode. I have made some minor changes during this process. I have my local setup ready now, I will soon be sending out smaller PRs as mentioned.

1reaction
girip11commented, Aug 17, 2020

Thanks @MarcoGorelli . I updated to latest release (0.1.22) and I didn’t face this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nbQA Documentation
Now, you'll be able to run the command from the previous section with just nbqa black notebook.ipynb. Much simpler!
Read more >
python - Why can I import successfully without __init__.py?
While looking for a module or package named "foo", for each directory in the parent path: If <directory>/foo/__init__.py is found, a regular ...
Read more >
How can I import code from the parent directory into ... - Reddit
If you run this before importing 'my_package', the notebook will be able to import utils.py - (you'll need an empty __init__.py file in...
Read more >
nbqa - PyPI
Run any standard Python code quality tool on a Jupyter Notebook.
Read more >
How to Get NBA Data Using the nba_api Python Module ...
After this tutorial, you should be able to install the nba_api module ... look a few other starter notebooks that you can use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found