nbQA-dev / nbQA

Run ruff, isort, pyupgrade, mypy, pylint, flake8, and more on Jupyter Notebooks
https://nbqa.readthedocs.io/en/latest/index.html
MIT License
1.04k stars 41 forks source link

Autopep8 corrupting notebooks with !pip command #696

Closed subpath closed 2 years ago

subpath commented 2 years ago

autopep8 changing order of import and !pip install command so notebook became corrupted because I want my !pip install command to be the first cell in the notebook.

Is there some parameter that I can use with autopep8 to avoid it?

Cheers : )

Steps to recreate

before:

# cell 1
!pip install -q pandas
# cell 2
import pandas as pd

after:

# cell 1
import pandas as pd
!pip install -q pandas

pre-commit-config.yaml

repos:
  - repo: https://github.com/nbQA-dev/nbQA
    rev: 1.2.2
    hooks:
      - id: nbqa-autopep8
MarcoGorelli commented 2 years ago

Thanks @subpath for the report - this feels like a bug, I'll have a look

MarcoGorelli commented 2 years ago

@subpath having looked into this furter, I don't think there's a bug

I don't think there's a way to turn off reording imports in autopep8 (as far as I can tell, at least), so I'd suggest using a cell tag to skip the first cell

Do let me know if that doesn't work for you and I'll reopen - thanks again for the report though, much appreciated