Cannot import name 'QuantStub' from ''

I was able to creaet a fastai 2.5.x / Python 3.9 conda env using:

  • conda env create -n fastbook -c defaults python=3.9
  • conda config --env --add channels defaults
  • conda config --env --add channels pytorch
  • conda config --env --add channels fastai
  • mamba install ipywidgets matplotlib pandas scikit-learn sentencepiece
  • pip install -r requirements.txt

The last command downgraded the mamba-installed torch 1.11 to 1.10, in case this matters.

When trying to import fastbook I get the following error:

----> 1 import fastbook

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/fastbook/, in <module>
      1 __version__ = "0.0.18"
      2 import matplotlib as mpl, pkgutil, requests, time
----> 3 from import *
      4 from fastdownload import download_url
      5 from pandas.api.types import CategoricalDtype

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/fastai/vision/, in <module>
----> 1 from . import models
      2 from ..basics import *
      3 from ..callback.all import *

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/fastai/vision/models/, in <module>
----> 1 from . import xresnet
      2 from . import unet
      3 from .tvm import *

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/fastai/vision/models/, in <module>
     11 # Cell
     12 from ...torch_basics import *
---> 13 try: from torchvision.models.utils import load_state_dict_from_url
     14 except ModuleNotFoundError: from torch.hub import load_state_dict_from_url
     16 # Cell

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/torchvision/, in <module>
      5 from torchvision import datasets
      6 from torchvision import io
----> 7 from torchvision import models
      8 from torchvision import ops
      9 from torchvision import transforms

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/torchvision/models/, in <module>
     16 from . import feature_extraction
     17 from . import optical_flow
---> 18 from . import quantization
     19 from . import segmentation
     20 from . import video

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/torchvision/models/quantization/, in <module>
----> 1 from .mobilenet import *
      2 from .resnet import *
      3 from .googlenet import *

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/torchvision/models/quantization/, in <module>
----> 1 from .mobilenetv2 import QuantizableMobileNetV2, mobilenet_v2, __all__ as mv2_all
      2 from .mobilenetv3 import QuantizableMobileNetV3, mobilenet_v3_large, __all__ as mv3_all
      4 __all__ = mv2_all + mv3_all

File ~/miniconda3/envs/fastbook/lib/python3.9/site-packages/torchvision/models/quantization/, in <module>
      3 from torch import Tensor
      4 from torch import nn
----> 5 from import QuantStub, DeQuantStub
      6 from torchvision.models.mobilenetv2 import InvertedResidual, MobileNetV2, model_urls
      8 from ..._internally_replaced_utils import load_state_dict_from_url

ImportError: cannot import name 'QuantStub' from '' (/home/maye/miniconda3/envs/fastbook/lib/python3.9/site-packages/torch/ao/quantization/

Anybody seen this and got around it?

what’s your torchvision version? perhaps need update or downgrade it…

Same version, but pip removed pytorch, weirdly…
Some funny stuff is happening there, with different channels and dependency breakages between pip and conda.
I’ll try to summarize what I currently have found is a complete set of instructions to make import fastbook work:

  1. conda create -n fastbook -c defaults python=3.9 -y
  2. conda activate fastbook
    2a: conda config --env --add channels defaults (if not yet set, my default is conda-forge)
    2b: conda config --env --add channels pytorch
    2c: conda config --env --add channels fastai
    (don’t add fastchan as a channel, and don’t install from there, it leads to mamba not finding dependencies for everything)
  3. mamba install pytorch torchvision ipywidgets matplotlib pandas scikit-learn sentencepiece -y
  4. pip install fastai graphviz azure-cognitiveservices-search-imagesearch
    (this has removed pytorch which will lead to above error, so it needs to be back-installed:)
  5. mamba install pytorch -y

and finally, import fastbook succeeds. I could run the first learning example from fastbook with that (around 1 minute on my Linux PC with NVIDIA GPU).

1 Like