简体   繁体   English

找不到文件 Jupyter 笔记本

[英]File not found Jupyter notebook

I am having trouble loading a file in jupyter notebook.我在 jupyter notebook 中加载文件时遇到问题。

Here is my project tree:这是我的项目树:

-- home - 家

---- cdsw ---- CDSW

------ my_main.py ------ my_main.py

------ notebooks ------ 笔记本

-------- my_notebook.ipynb -------- my_notebook.ipynb

------ dns ------ dns

-------- assets --------资产

---------- stopwords.txt ---------- 停用词.txt

-------- bilans -------- 比兰斯

---------- my_module.py ---------- my_module.py

Know that '/home/cdsw/" is in my PYTHONPATH - the same interpreter in which I launch jupyter -.知道 '/home/cdsw/" 在我的 PYTHONPATH 中——我在其中启动 jupyter 的同一个解释器。

In my_module.py I have these lines:在 my_module.py 我有这些行:

PATH_STOPWORDS: Final = os.path.join("dns", "assets", "stopwords.txt")
STOPWORDS: Final = load_stopwords(PATH_STOPWORDS)

load_stopwords is basically just a open(PATH_STOPWORDS, 'r'). load_stopwords 基本上只是一个开放的(PATH_STOPWORDS,'r')。 So my problem is that when I import dns.bilans.my_module inside my_main.py it works fine: file is correctly loaded.所以我的问题是,当我在 my_main.py 中导入 dns.bilans.my_module 时,它工作正常:文件已正确加载。 Yet, when I import it from my_notebook.ipynb, it does not:然而,当我从 my_notebook.ipynb 导入它时,它不会:

FileNotFoundError: [Errno 2] No such file or directory: 'dns/assets/stopwords.txt'

So my_module is indeed founded by jupyter kernel (because it reads the code lines of the file) but can't use the relative path provided like it does from a run in a terminal.所以 my_module 确实是由 jupyter kernel 创建的(因为它读取文件的代码行)但不能像在终端中运行那样使用提供的相对路径。

When I use a open(relpath, 'r') inside a module, I don't need to go all through the project tree right?当我在模块中使用 open(relpath, 'r') 时,我不需要 go 遍历整个项目树,对吗? Indeed it DOES work in my_main.py...事实上它确实在 my_main.py 中工作......

I really don't get it...我真的不明白...

The output of os.getcwd() in jupyter is "/home/cdsw/notebooks". jupyter中os.getcwd()的output是“/home/cdsw/notebooks”。

This existing SO question suggests how to find files relative to the position of a Python code file. This existing SO question建议如何查找与 Python 代码文件的 position 相关的文件。 It isn't exactly the same question, however, and I believe that this technique is so important for every Python programmer to understand, that I'm going to provide a more thorough answer.然而,这不是完全相同的问题,我相信这项技术对于每个 Python 程序员来说都非常重要,因此我将提供更详尽的答案。

Given a piece of Python code, one can compute the path of the directory of the source file containing that code via:给定一段 Python 代码,可以通过以下方式计算包含该代码的源文件目录的路径:

here = os.path.dirname(__file__)

Having the position of the relevant source file, it is easy to compute an absolute path to any data file that has a well known location relative to that source file.有了相关源文件的 position,就可以很容易地计算出相对于该源文件具有众所周知位置的任何数据文件的绝对路径。 In this case, the way to do that is:在这种情况下,这样做的方法是:

stopwords_path = os.path.join(here, '..', '..', 'assets', 'stopwords.txt')

This path can be supplied to open() or used in any other way to refer to the stopwords.txt data file.此路径可以提供给open()或以任何其他方式使用以引用stopwords.txt数据文件。 Here, the way to use this path would be:在这里,使用此路径的方式是:

load_stopwords(stopwords_path)

I use this technique to not only find files that accompany code in a particular module, but also to find files that are in other locations throughout my source tree.我使用这种技术不仅可以找到特定模块中代码附带的文件,还可以找到整个源代码树中其他位置的文件。 As long as the code and data file exist in the same source repository, or are shipped together in a single Python package, the relative path will not change from installation to installation, and so this technique will work.只要代码和数据文件存在于同一个源存储库中,或者在单个 Python package 中一起提供,相对路径就不会在安装之间发生变化,因此该技术将起作用。

In general, you should avoid the use of relative paths.通常,您应该避免使用相对路径。 Whenever possible, you should also avoid having to tell your code where to find something.只要有可能,您还应该避免告诉您的代码去哪里找东西。 For any situation, ask yourself how you can obtain a reliable absolute path that you can then use to then locate whatever it is you're wanting to access.在任何情况下,问问自己如何才能获得可靠的绝对路径,然后您可以使用它来定位您想要访问的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM