ImportError: cannot import name 'datasets' from 'fastai'

Hi there, I’ve been looking all over for similar posts but can’t seem to find anything that fits my issue. I’ve run the following in order to set up a local Jupyter environment using Anaconda:

conda create --name fastai
conda activate fastai
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=fastai
conda install nb_conda

This runs without any visible hitch, but when opening a new notebook and running the following line:

from fastai import datasets

I get the error in the title of the post. Sure enough, if I inspect the __init__.py in the error source, which points to the local fastai installation, the file is empty except for the declaration of the version number. I’m not sure how to fix this error, any help would be greatly appreciated!

I’m running on Mac Catalina, version 10.15.7 if this helps

You are creating an environment called fastai there, but you aren’t actually installing fastai from what I can see. I would try just doing import fastai to confirm that and if that is confirmed, you should do

conda install -c fastchan fastai anaconda

Thanks Kevin.
I tried that but it still complains; kindly note that I am also using an Apple OS: BigSur 11.4

Does it need a patch of some type? because FastAi 2.4.1 is the version which seems installed. Here the error. Please, help. Thanks!

ImportError Traceback (most recent call last)
in
2 from pathlib import Path
3 from IPython.core.debugger import set_trace
----> 4 from fastai import datasets
5 import pickle, gzip, math, torch, matplotlib as mpl
6 import matplotlib.pyplot as plt

ImportError: cannot import name ‘datasets’ from ‘fastai’ (/Users/andrea/opt/anaconda3/lib/python3.8/site-packages/fastai/init.py)

That’s expected behavior for version 2.4.1. I wonder if your code expects fastai version 1.

Thank you for your answer
I think that the main problem is that it may have some security issue of the OS. Since I am using Mac BigSur 11.4 but I am unable to locate this.

For now, I just tried for now to re-run this from the course dl2:

#export
from pathlib import Path
from IPython.core.debugger import set_trace
from fastai import datasets
import pickle, gzip, math, torch, matplotlib as mpl
import matplotlib.pyplot as plt
from torch import tensor

and it stops where I import datasets from fastai. If I substitute
’ from fastai import datasets ’ with ’ from fastai import * ’
it complains at the next line:

path = datasets.download_data(MNIST_URL, ext=’.gz’); path

NameError Traceback (most recent call last)
in
----> 1 path = datasets.download_data(MNIST_URL, ext=’.gz’); path

NameError: name ‘datasets’ is not defined

I’m also running Mac Big Sur, and I don’t think you have a security issue. I think you have a version mismatch issue. The code you’re attempting to run appears to expect an earlier version of fastai. When I run that same code excerpt on my machine, I get the same error, which I expect.

I think you need to clarity what version of fastai your code expects, or whether you want to modify your code for the current version (2.4.1).

For example, you should be able to use

from fastai.data.all import *
path = untar_data(URLs.MNIST)

to download the data your current code is erroring on. But again, you’ll run into more version errrors further down in your code unless you figure out whether you 1) want to use an earlier version of fastai or 2) update the code to use the current version. I’d recommend 2).

1 Like

Thank you very much for this detailed explanation

I just got this code from the lessons of course v3 part 2
I agree that indeed there may be other issues. At least now I know which is the problem.

I will try to update the code as I get along in the course. Thanks!

I hope not to need to revert to v1 to make it work streamline. I would be surprised since this is what is in the notebooks of the course ‘from the foundations’ of v3. In case, is there a way that you are aware of to have two versions installed?

from fastai.data.all import *
path = untar_data(URLs.MNIST)

This works for me. I am using fastai version 2.4.1