path problem : NameError: name '__file__' is not defined
Asked Answered
B

2

7
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!

Boisterous answered 25/8, 2020 at 16:16 Comment(1)
Related questionKnute
V
13

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

Verbose answered 13/11, 2020 at 19:51 Comment(0)
D
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.

Democracy answered 25/8, 2020 at 16:26 Comment(2)
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.Boisterous
__file__ is optional. Seems Jupiter doesn't set the var in a notebook.Democracy

© 2022 - 2024 — McMap. All rights reserved.