6
import os.path as osp
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.datasets import MNISTSuperpixels
import torch_geometric.transforms as T
from torch_geometric.data import DataLoader
from torch_geometric.utils import normalized_cut
from torch_geometric.nn import (NNConv, graclus, max_pool, max_pool_x, global_mean_pool)

path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'MNIST')

transform = T.Cartesian(cat=False)
train_dataset = MNISTSuperpixels(path, True, transform=transform)
test_dataset = MNISTSuperpixels(path, False, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
d = train_dataset

I'm trying to use MNISTSuperpixels data for graph convolution, but I have some troubles using the example code. Most of scripts were using path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'MNIST') However, they gave me an error NameError: name '__file__' is not defined and I don't understand what osp.realpath(__file__) really means.

I'm using Jupyter notebook on Ubuntu, and my working directory is

print(os.getcwd())
/home/hkimlx/GDL/pytorch_geometric/examples

which is the same directory where the sample code mnist_nn_conv.py is located.

Please help me. Thanks!

mario119
  • 343
  • 3
  • 14

2 Answers2

11

In notebook, you need to use double quoted "__file__" as in osp.realpath("__file__") instead of osp.realpath(__file__)

Sourced from: https://queirozf.com/entries/python-working-with-paths-the-filesystem#-nameerror-name-'file'-is-not-defined

peng
  • 111
  • 1
  • 3
1

Per the documentation

__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

According the answer you can't get the path of a notebook pragrammatically. Use os.getcwd() as a workaround.

2pizza
  • 101
  • 5
  • What does 'loaded from a file' mean? Based on your saying, I cannot solve this error while using jupyter notebook? but I have the same error when I use command prompt. – mario119 Aug 25 '20 at 16:42
  • `__file__` is optional. Seems Jupiter doesn't set the var in a notebook. – 2pizza Aug 25 '20 at 16:46