本文介绍了路径问题:NameError:未定义名称"__file__"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试使用MNISTSuperpixels数据进行图卷积,但是在使用示例代码时遇到了一些麻烦.大多数脚本都在使用 path = osp.join(osp.dirname(osp.realpath(__ file__)),'..','data','MNIST')但是,他们给了我一个错误 NameError:名称'__file__'未定义,我不理解 osp.realpath(__ file __)的真正含义.

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

我在Ubuntu上使用Jupyter笔记本,我的工作目录为

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

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

与示例代码 mnist_nn_conv.py 所在的目录相同.

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

请帮助我.谢谢!

推荐答案

在笔记本中,您需要使用双引号"__file__"就像 osp.realpath("__ file __")一样,而不是 osp.realpath(__ file __)

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

来源: https://queirozf.com/entries/python-working-with-paths-the-filesystem#-nameerror-name-'文件'-未定义

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

这篇关于路径问题:NameError:未定义名称"__file__"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 02:33