使用Python 3从Jupyter Notebook中相对导入的另一个目录中的模块导入本地函数 [英] Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3

查看:1062
本文介绍了使用Python 3从Jupyter Notebook中相对导入的另一个目录中的模块导入本地函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有类似于以下内容的目录结构

I have a directory structure similar to the following

meta_project
    project1
        __init__.py
        lib
            module.py
            __init__.py
    notebook_folder
        notebook.jpynb

notebook.jpynb中工作时,如果尝试通过以下方式使用相对导入来访问module.py中的功能function():

When working in notebook.jpynb if I try to use a relative import to access a function function() in module.py with:

from ..project1.lib.module import function

我收到以下错误:

SystemError                               Traceback (most recent call last)
<ipython-input-7-6393744d93ab> in <module>()
----> 1 from ..project1.lib.module import function

SystemError: Parent module '' not loaded, cannot perform relative import

有什么方法可以使用相对导入来使它正常工作吗?

Is there any way to get this to work using relative imports?

请注意,笔记本服务器是在meta_project目录级别实例化的,因此它应有权访问这些文件中的信息.

Note, the notebook server is instantiated at the level of the meta_project directory, so it should have access to the information in those files.

还请注意,至少不像最初打算的project1那样被认为是模块,因此没有__init__.py文件,它只是作为文件系统目录.如果要解决问题,则需要将其视为模块,并包含一个很好的__init__.py文件(甚至是空白文件),但这样做不足以解决问题.

Note, also, that at least as originally intended project1 wasn't thought of as a module and therefore does not have an __init__.py file, it was just meant as a file-system directory. If the solution to the problem requires treating it as a module and including an __init__.py file (even a blank one) that is fine, but doing so is not enough to solve the problem.

我在计算机之间共享此目录,并且相对导入允许我在任何地方使用相同的代码,&我经常使用笔记本电脑进行快速原型制作,因此涉及将绝对路径捆绑在一起的建议不太可能有帮助.

I share this directory between machines and relative imports allow me to use the same code everywhere, & I often use notebooks for quick prototyping, so suggestions that involve hacking together absolute paths are unlikely to be helpful.

这与 Python 3中的相对导入不同,后者谈论相对通常从Python 3中导入,尤其是从程序包目录中运行脚本.这与在jupyter笔记本中尝试在另一个具有不同常规和特定方面的本地目录中的本地模块中调用函数有关.

This is unlike Relative imports in Python 3, which talks about relative imports in Python 3 in general and – in particular – running a script from within a package directory. This has to do with working within a jupyter notebook trying to call a function in a local module in another directory which has both different general and particular aspects.

推荐答案

I had almost the same example as you in this notebook where I wanted to illustrate the usage of an adjacent module's function in a DRY manner.

我的解决方案是通过向笔记本中添加如下代码片段来告诉Python该额外的模块导入路径:

My solution was to tell Python of that additional module import path by adding a snippet like this one to the notebook:

import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)

这允许您从模块层次结构中导入所需的功能:

This allows you to import the desired function from the module hierarchy:

from project1.lib.module import function
# use the function normally
function(...)

请注意,如果还没有空的__init__.py文件,则必须将它们添加到 project1/ lib/文件夹中.

Note that it is necessary to add empty __init__.py files to project1/ and lib/ folders if you don't have them already.

这篇关于使用Python 3从Jupyter Notebook中相对导入的另一个目录中的模块导入本地函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆