Repository: pytorch
Issue: Dataloader multiprocess loading with num_worker > 0 calls __main__ file to run
Labels: module: dataloader,triaged

### 🐛 Describe the bug

A conditional bug -- All the imports are used apart from tqdm
I am not using tqdm directly But if I don't use it code never runs, for more [reference](https://discuss.pytorch.org/t/error-while-multiprocessing-in-dataloader/46845/27)

When I import it, it sometimes runs but not always.
But when it runs, all print statement in the script are called. Other people solved this problem without understanding the problem in this [thread](https://discuss.pytorch.org/t/errors-when-using-num-workers-0-in-dataloader/97564/3)

Hope you can replicate the bug on your end because it seems to be very conditional!
```
#%%
import torch
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from pathlib import Path
import matplotlib.pyplot as plt
from tqdm import tqdm
import requests 
import zipfile
from pathlib import Path
import os
data_path = Path("data/")

image_path = data_path/ 'pizza_steak_sushi'

if not image_path.is_dir():
    print(f'{image_path} does not exist, creating one ...')
    image_path.mkdir(parents=True, exist_ok=True)

    url = 'https://github.com/mrdbourke/pytorch-deep-learning/raw/main/data/pizza_steak_sushi.zip'
    local_zip_file = data_path / 'pizza_steak_sushi.zip'
    
    print(f'Downloading pizza, steak, sushi data from {url}')
    response = requests.get(url)
    with open(local_zip_file, 'wb') as f:
        f.write(response.content)

    with zipfile.ZipFile(local_zip_file, 'r') as zip_ref:
        print('Unzipping pizza, steak, sushi data ...')
        zip_ref.extractall(image_path)

    os.remove(local_zip_file)
    print(f'Removed {local_zip_file} after extraction')
else:
    print(f'{image_path} directory already exists, skipping download')
#%%

data_path = Path("data/")
image_path = data_path/ 'pizza_steak_sushi'

train_dir = image_path / "train"
test_dir = image_path / "test"
device = 'cuda' if torch.cuda.is_available() else 'cpu'

data_transforms = transforms.Compose([
    transforms.Resize(size=(64,64)),
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ToTensor(),
])

train_data = datasets.ImageFolder(root= train_dir,
                                  transform=data_transforms,
                                  target_transform=None)
# %%
from torch.utils.data import DataLoader
BATCH_SIZE = 1
train_dataloader = DataLoader(dataset=train_data,
                              batch_size=BATCH_SIZE,
                              num_workers=1,
                              shuffle=True)
test_dataloader = DataLoader(dataset=test_data,
                              batch_size=BATCH_SIZE,
                              num_workers=1,
                              shuffle=False)
len(train_dataloader), len(test_dataloader)
#%%
img, label = next(iter(train_dataloader))
print(f"Image shape: {img.shape} -> [batch_size, height, width]")
print(f"Label shape: {label.shape}")

print("Why are you running the full script")
# %%
```

![Screenshot 2023-04-25 at 20 12 58](https://user-images.githubusercontent.com/23452166/234379882-44028a23-7f13-4627-af54-2fcf52c047e6.png)


### Versions

Collecting environment information...
PyTorch version: 1.13.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N[/A](https://file+.vscode-resource.vscode-cdn.net/A)

OS: macOS 13.3.1 (x86_64)
GCC version: Could not collect
Clang version: Could not collect
CMake version: Could not collect
Libc version: N[/A](https://file+.vscode-resource.vscode-cdn.net/A)

Python version: 3.9.15 (main, Nov  4 2022, 11:11:31)  [Clang 12.0.0 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N[/A](https://file+.vscode-resource.vscode-cdn.net/A)
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N[/A](https://file+.vscode-resource.vscode-cdn.net/A)
MIOpen runtime version: N[/A](https://file+.vscode-resource.vscode-cdn.net/A)
Is XNNPACK available: True

CPU:
Apple M1

Versions of relevant libraries:
[pip3] flake8==4.0.1
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.5
[pip3] numpydoc==1.5.0
[pip3] torch==1.13.1
[pip3] torchmetrics==0.11.4
[pip3] torchvision==0.14.1
[conda] blas                      1.0                         mkl  
[conda] ffmpeg                    4.3                  h0a44026_0    pytorch
[conda] mkl                       2021.4.0           hecd8cb5_637  
[conda] mkl-service               2.4.0            py39h9ed2024_0  
[conda] mkl_fft                   1.3.1            py39h4ab4a9b_0  
[conda] mkl_random                1.2.2            py39hb2f4e1b_0  
[conda] numpy                     1.21.5           py39h2e5f0a9_3  
[conda] numpy-base                1.21.5           py39h3b1a694_3  
[conda] numpydoc                  1.5.0            py39hecd8cb5_0  
[conda] pytorch                   1.13.1                  py3.9_0    pytorch
[conda] torchmetrics              0.11.4                   pypi_0    pypi
[conda] torchvision               0.14.1                 py39_cpu    pytorch

cc @SsnL @VitalyFedyunin @ejguan @NivekT @dzhulgakov