In Google Colab: AttributeError: 'PosixPath' object has no attribute 'ls'

I’m following lesson 5 in Google Colab, trying to use Path, but I’m getting the following error:

AttributeError: 'PosixPath' object has no attribute 'ls'

From:

path = Path('/content/drive/My Drive/data/mnist')
path.ls()

My guess is that this has something to do with Colab?

So, DataBunch wasn’t working either. Even though I had imported:

from fastai import *

Is this normal? It does not align with the course notebook. Anyways, then I tried importing the fastai.vision application and everything worked. Could this be a bug with from fastai import *?

Try doing from fastai.vision import *

Also are you importing pathlib at any point

Yeah, like I said in the second comment, I used fastai.vision and it worked, but I’m confused as to why from fastai import * doesn’t work.

I didn’t import pathlib at first and tried to afterwards. It didn’t work, but that’s because ls() is not part of pathlib, it’s an add-on in fastai.

Because some of the modules live in fastai and some in fastai.x based on specific interactions in the library (this is just how it’s structured).

1 Like

Yeah, this makes sense! It’s just that there seems to have been a change in the fastai library structure since the the Part 1 course. Jeremy was using from fastai import * and not from fastai.vision import *.

Correct, but the minimal way of doing it is:

from fastai import core

because it’s implemented in core.py:239

Path.ls = lambda x: list(x.iterdir())

(if we’re talking about Fastai v1, I’m relatively new to v2…)

1 Like

Note to self, in Fast.ai v2 now the patching happens in fastcore

from fastcore import all

(sorry for the necroposting, I found this thread while trying to solve this problem and also found my own answer… it looked a good idea to update the answer)