"ImportError: cannot import name 'BatchSampler'"

Using
conda install -y --name fastai$PYTHON_VERSION -c soumith pytorch=0.3.0
soloved my problem where fastai is your conda env.

I copied https://github.com/pytorch/pytorch/blob/master/torch/_six.py and https://github.com/pytorch/pytorch/blob/master/torch/utils/data/sampler.py and it worked. My os was debian without GPU.

I am also having this problem in Paperspace. Any ideas would be greatly appreciated. I’ve updated my conda and did a git pull

3 Likes

I also have the same problem with a newly created Paperspace machine (after running “git pull” and “conda env update”).

(fastai) paperspace@psetuga1v:~/fastai$ python -c “from fastai import *”
Traceback (most recent call last):
File “<string>”, line 1, in <module>
File “/home/paperspace/fastai/fastai/init.py”, line 1, in <module> from .basic_train import *
File “/home/paperspace/fastai/fastai/basic_train.py”, line 2, in <module> from .torch_core import *
File “/home/paperspace/fastai/fastai/torch_core.py”, line 2, in <module> from .imports.torch import *
File “/home/paperspace/fastai/fastai/imports/torch.py”, line 4, in <module> from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, TensorDataset
ImportError: cannot import name 'BatchSampler

Ran into the same error. I think it’s got to do with the move to the rewritten fastai library.
What fixed it for me was just reverting to an earlier, working commit by issuing:
git checkout 2e1ccb58121dc648751e2109fc0fbf6925aa8887

That puts the repo back to an earlier stage which gets everything working for me again. Note that this isn’t a resilient fix: if you intend to work on the fastai library code, this definitely wouldn’t be the way to go. Then again, if that’s the case, you probably know enough to fix the underlying root cause.

2 Likes

I reset the symbolic link to point to …/fastai/old/fastai/ (old library). Then, it works again. I also have the latest library for the upcoming course.

3 Likes

I am using Google colab for setting up Fastai, how can i resolve this issue in colab…

You can pip install from github repository, choosing your preferred branch or commit.

!pip install git+GITHUBADDRESS@NAMEOFTHEBRANCH

or

!pip install git+GITHUBADDRESS@COMMIT

So for example, in Laurenth case it should be

!pip install git+https://github.com/fastai/fastai@2e1ccb58121dc648751e2109fc0fbf6925aa8887

Personally I used the following, and solved that error, but a new one raised

!pip install git+https://github.com/fastai/fastai@ulmfit_cleanup

It returned:

AttributeError: module 'torch.nn' has no attribute 'functional'

but that’s another story…

Thank you,

It seems to be the best option for now as they changed a lot to the fastai repo to use pytorch 0.4.1 features, but the setup process still using 0.3.1. Installing 0.4.1 is an option but in the future who knows what might break.

1 Like

@jakelong Can you please help me in installing fastai library. I have followed all the steps mentioned in the forum

But when I import fastai module I am getting the error.

Thanks,
Ritika

It seems like you already had your fastai conda env set up. In the step 8 of the howto you can see the instruction as:
cd courses\dl1
del fastai
mklink /d fastai ..\..\fastai
cd ..\..

If you already did the above things, open an anaconda prompt, cd into fastai folder, activate fastai env and run these
cd courses\dl1
rmdir fastai
mklink /d fastai ..\..\old\fastai
cd ..\..

Don’t try to import fastai from python started in the command line, run jupyter notebook and open lession1.ipynb in courses/dl1 and run the first 6 blocks of code. It should run without problem and torch.cuda.is_available() and torch.backends.cudnn.enabled should both return True.

Hope this help

Hoping to be helpful here I shared a Google Colab notebook ready to go, in order to make fast.ai course ready to go to anyone.
It has comments about common issues I solved too.
You are free to comment or fork it and let me/us know if/how you’ll improve it.

Hi Jake,

Thank you for the quick response. I tried to followed the instructions shared by you.
I got the error.Please suggest how to solve this error.


Thanks,
Ritika

Thanks ! This will save a lot of time.Waiting notes for other lessons too.

In my case I enabled Developer mode in windows by opening start menu and search for developers settings, however I’m using windows 10 pro and not sure if you have the same. Also I saw that you ran del fastai, which doesn’t remove the symlink but rather delete files in fastai folder, instead of rmdir fastai. You should check to see if you had deleted any files in that folder and revert changes made to it, then run only the second bunch of code in my previous post, not the first one.

Hello, i pointed it to the old fastai but now in the jupyter notebook it tells me it can not find imports.

do you have an imports folder in /old/fastai? thanks

Hi Jake,

I tried to replicate the same steps.Still I am facing the issues. I am not able to import fastai library.Please suggest how to proceed.

Thanks,
Ritika

No. If you can’t import. It is possibly related to the folder structure. Make sure the symbolic link is point to your working directory.

1 Like

can you post the error so we can have a look please