__file__ 在 Jupyter Notebook 中不存在
__file__ does not exist in Jupyter Notebook
我在 Jupyter Notebook 服务器上 (v4.2.2) Python 3.4.2 和
我想使用全局名称 __file__
,因为笔记本将从其他用户那里克隆,在一个部分中我必须 运行:
def __init__(self, trainingSamplesFolder='samples', maskFolder='masks'):
self.trainingSamplesFolder = self.__getAbsPath(trainingSamplesFolder)
self.maskFolder = self.__getAbsPath(maskFolder)
def __getAbsPath(self, path):
if os.path.isabs(path):
return path
else:
return os.path.join(os.path.dirname(__file__), path)
__getAbsPath(self, path)
检查 path
参数是相对路径还是绝对路径,returns 是 path
的绝对版本。所以我可以稍后安全地使用返回的path
。
但是我得到了错误
NameError: name '__file__'
is not defined
我在网上搜索了这个错误,发现"solution"我应该更好地使用sys.argv[0]
,但是print(sys.argv[0])
returns
/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py
但正确的笔记本位置应该是 /home/ubuntu/notebooks/
。
感谢 Martijn Pieters 的参考 How do I get the current IPython Notebook name(评论)最后的答案(未接受)非常适合我的需要:
print(os.getcwd())
/home/ubuntu/notebooks
无法获取笔记本的路径。您可能会找到一种方法来获得它,它只适用于一种环境(例如 os.getcwd()
),但如果笔记本以不同的方式加载,它就不一定有效。
相反,尝试编写笔记本,这样它就不需要知道自己的路径。如果做一些类似获取密码的事情,那么一定要快速失败/如果这不起作用则打印错误,而不是静静地尝试继续。
在现代 Python (v3.4+) 中,我们可以使用 pathlib 来获取笔记本的目录:
from pathlib import Path
cwd = Path().resolve()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')
# or this way, thanks @NunoAndré:
cwd = Path.cwd()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')
更新
@ShitalShah 我无法重现您报告的错误。 Jupyter 笔记本似乎工作正常,无论应用程序启动的当前工作目录如何。
示例:文件 ~/dir1/dir2/untitled.ipynb
和 Jupyter notebook 在 ~/dir1
中启动:
Jupyter 笔记本开始于 ~/dir1/dir2
:
__file__
可能对您不可用,但实际上您可以通过不同的方式获取笔记本所在的当前文件夹。
全局变量中有踪迹,如果你调用globals()
你会看到有一个键为_dh
的元素,这可能对你有帮助。在这里,我如何设法加载与我的笔记本位于同一文件夹中的 data.csv
文件:
import os
current_folder = globals()['_dh'][0]
# Calculating path to the input data
data_location = os.path.join(current_folder,'data.csv')
如果您想获取脚本所在目录的路径 运行,我强烈建议您使用
os.path.abspath('')
优势
- 它适用于 Jupyter Notebook
- 它在 REPL 中工作
- 不需要Python 3.4的路径库
请注意,__file__
有优势的一种情况是当您从目录 A 调用 python 但在目录 B 中调用 运行 脚本时。在上述情况以及大多数情况下其他方法将 return A,而不是 B。但是对于 Jupyter notbook,您总是获得 .ipyn
文件的文件夹,而不是启动 jupyter notebook
.
的目录
我在 Jupyter Notebook 服务器上 (v4.2.2) Python 3.4.2 和
我想使用全局名称 __file__
,因为笔记本将从其他用户那里克隆,在一个部分中我必须 运行:
def __init__(self, trainingSamplesFolder='samples', maskFolder='masks'):
self.trainingSamplesFolder = self.__getAbsPath(trainingSamplesFolder)
self.maskFolder = self.__getAbsPath(maskFolder)
def __getAbsPath(self, path):
if os.path.isabs(path):
return path
else:
return os.path.join(os.path.dirname(__file__), path)
__getAbsPath(self, path)
检查 path
参数是相对路径还是绝对路径,returns 是 path
的绝对版本。所以我可以稍后安全地使用返回的path
。
但是我得到了错误
NameError: name
'__file__'
is not defined
我在网上搜索了这个错误,发现"solution"我应该更好地使用sys.argv[0]
,但是print(sys.argv[0])
returns
/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py
但正确的笔记本位置应该是 /home/ubuntu/notebooks/
。
感谢 Martijn Pieters 的参考 How do I get the current IPython Notebook name(评论)最后的答案(未接受)非常适合我的需要:
print(os.getcwd())
/home/ubuntu/notebooks
无法获取笔记本的路径。您可能会找到一种方法来获得它,它只适用于一种环境(例如 os.getcwd()
),但如果笔记本以不同的方式加载,它就不一定有效。
相反,尝试编写笔记本,这样它就不需要知道自己的路径。如果做一些类似获取密码的事情,那么一定要快速失败/如果这不起作用则打印错误,而不是静静地尝试继续。
在现代 Python (v3.4+) 中,我们可以使用 pathlib 来获取笔记本的目录:
from pathlib import Path
cwd = Path().resolve()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')
# or this way, thanks @NunoAndré:
cwd = Path.cwd()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')
更新
@ShitalShah 我无法重现您报告的错误。 Jupyter 笔记本似乎工作正常,无论应用程序启动的当前工作目录如何。
示例:文件 ~/dir1/dir2/untitled.ipynb
和 Jupyter notebook 在 ~/dir1
中启动:
Jupyter 笔记本开始于 ~/dir1/dir2
:
__file__
可能对您不可用,但实际上您可以通过不同的方式获取笔记本所在的当前文件夹。
全局变量中有踪迹,如果你调用globals()
你会看到有一个键为_dh
的元素,这可能对你有帮助。在这里,我如何设法加载与我的笔记本位于同一文件夹中的 data.csv
文件:
import os
current_folder = globals()['_dh'][0]
# Calculating path to the input data
data_location = os.path.join(current_folder,'data.csv')
如果您想获取脚本所在目录的路径 运行,我强烈建议您使用
os.path.abspath('')
优势
- 它适用于 Jupyter Notebook
- 它在 REPL 中工作
- 不需要Python 3.4的路径库
请注意,__file__
有优势的一种情况是当您从目录 A 调用 python 但在目录 B 中调用 运行 脚本时。在上述情况以及大多数情况下其他方法将 return A,而不是 B。但是对于 Jupyter notbook,您总是获得 .ipyn
文件的文件夹,而不是启动 jupyter notebook
.