AttributeError: Can't pickle local object 'pre_datasets..' when implementing Pytorch framework
I was trying to implement a pytorch framework on CNN. 
I'm sure the code is right because it's from a tutorial and it works when I ran it on Jupyter Notebook on GoogleDrive. 
But when I tried to localize it as a .py file, it suggest an error:
AttributeError: Can't pickle local object 'pre_datasets.&lt;locals&gt;.&lt;lambda&gt;'
I know it's about inferencing objects outside a function, but what was the exact matter about this error? 
And how should I fix it?

Here's the major part of the code.
def pre_datasets():
    TRAIN_TFM = transforms.Compose(
        [
            transforms.Resize(size=(128, 128)),
            # TODO
            transforms.ToTensor(),
        ]
    )
    train_set = DatasetFolder(
        root=CONFIG[&quot;train_set_path&quot;],
        loader=lambda x: Image.open(x),
        extensions=&quot;jpg&quot;,
        transform=TRAIN_TFM,
    )
    train_loader = DataLoader(
        dataset=train_set,
        batch_size=CONFIG[&quot;batch_size&quot;],
        shuffle=True,
        num_workers=CONFIG[&quot;num_workers&quot;],
        pin_memory=True,
    )
    return train_loader

def train(train_loader):
    ...
    for epoch in range(CONFIG[&quot;num_epochs&quot;]):
    ...
        for batch in train_loader: # error happened here
    ...

if __name__ == &quot;__main__&quot;:
    train_loader = pre_datasets()
    train(train_loader)

Here's the error message:
Traceback (most recent call last):
  File &quot;HW03_byCRZ.py&quot;, line 197, in &lt;module&gt;
    train(train_loader, valid_loader)
  File &quot;HW03_byCRZ.py&quot;, line 157, in train
    for batch in train_loader:
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/site-packages/torch/utils/data/dataloader.py&quot;, line 355, in __iter__
    return self._get_iterator()
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/site-packages/torch/utils/data/dataloader.py&quot;, line 301, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/site-packages/torch/utils/data/dataloader.py&quot;, line 914, in __init__
    w.start()
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/multiprocessing/process.py&quot;, line 121, in start
    self._popen = self._Popen(self)
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/multiprocessing/context.py&quot;, line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/multiprocessing/context.py&quot;, line 284, in _Popen
    return Popen(process_obj)
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/multiprocessing/popen_spawn_posix.py&quot;, line 32, in __init__
    super().__init__(process_obj)
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/multiprocessing/popen_fork.py&quot;, line 19, in __init__
    self._launch(process_obj)
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/multiprocessing/popen_spawn_posix.py&quot;, line 47, in _launch
    reduction.dump(process_obj, fp)
  File &quot;/Users/ceezous/opt/anaconda3/envs/pytorch_env/lib/python3.8/multiprocessing/reduction.py&quot;, line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'pre_datasets.&lt;locals&gt;.&lt;lambda&gt;'


I had a similar issue and I used dill like this:
import dill as pickle

and it worked out of the box!
