简体   繁体   English

无法从 Jupyter-lab notebook 和 pathlib.Path 导入位于父文件夹中的模块

[英]Can't import module situated in parent folder from Jupyter-lab notebook and pathlib.Path

Here's my situation.这是我的情况。 I have some jupyter notebooks inside some folder and I would like to share some code between those notebooks trough a library I made.我在某个文件夹中有一些 jupyter 笔记本,我想通过我制作的库在这些笔记本之间共享一些代码。

The folder structure is the following:文件夹结构如下:

1.FirstFolder/
    notebookA.ipynb
2.SecondFolder/
    notebookB.ipynb
mylib/
    __init__.py
    otherfiles.py

I tried putting the following code at the beginning of the notebook:我尝试将以下代码放在笔记本的开头:

# to use modules in parent folder
import sys
import os
from pathlib import Path
libpath = os.path.join(Path.cwd().parent,'mylib')
print(f"custom library functions are in the module:\n\t{libpath}")
sys.path.append(libpath)
import mylib

The print outputs the correct path of the module and then a ModuleNotFoundError comes up and the program crashes:打印输出模块的正确路径,然后出现 ModuleNotFoundError 并且程序崩溃:

---> 10 import mylib
     11 from mylib import *

ModuleNotFoundError: No module named 'mylib'

Looking up on SO I found that that should have been the way to import a module from a non-default folder.查找 SO 我发现这应该是从非默认文件夹导入模块的方法。 Where is the error?错误在哪里?

EDIT: after FinleyGibson's answer I tried sys.path.append(Path.cwd().parent) and restarted the kernel but I still have the same problem.编辑:在 FinleyGibson 的回答之后,我尝试了sys.path.append(Path.cwd().parent)并重新启动了内核,但我仍然sys.path.append(Path.cwd().parent)同样的问题。

EDIT2: I tried this and it worked, but I still would like to know why the previous approaches haven't worked. EDIT2:我试过这个并且它有效,但我仍然想知道为什么以前的方法没有奏效。

import sys
import os
from pathlib import Path
tmp = Path.cwd()
os.chdir(Path.cwd().parent)
sys.path.append(Path.cwd())
import mylib
from mylib.dataloading import *
os.chdir(tmp)

You have added the contents of os.path.join(Path.cwd().parent,'mylib') to your path, this means python will look inside this dir for the module you are importing.您已将os.path.join(Path.cwd().parent,'mylib')内容添加到您的路径中,这意味着 python 将此目录中查找您正在导入的模块。 mylib is not located in this dir, but rather the parent dir. mylib不在此目录中,而是位于父目录中。 Also Path.cwd().parent returns a pathlib.PosixPath object.此外Path.cwd().parent返回一个pathlib.PosixPath对象。 Convert this to a string to use it with import (or, just use sys.path.append('../') :将其转换为字符串以将其与导入一起使用(或者,只需使用sys.path.append('../')

try:尝试:

import sys
import os
from pathlib import Path
sys.path.append(str(Path.cwd().parent))
import mylib

doing this allows me to import a variable X = 'import success' located in otherfiles.py like so:这样做允许我像这样X = 'import success'位于 otherfiles.py 中的变量X = 'import success'

ans = mylib.otherfiles.X
print(ans)

>>> 'import success'

I think Jupiter is not able to locate the folder of your module.我认为 Jupiter 无法找到您模块的文件夹。

First cell use第一次使用细胞

cd ..

then in the next cell - then it should work然后在下一个单元格中 - 那么它应该可以工作

import mylib 

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

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