简体   繁体   English

如何将本地模块导入 azure databricks 笔记本?

[英]How to import a local module into azure databricks notebook?

I'm trying to use a module in databrick's notebook but I am completely blocked.我正在尝试在 databrick 的笔记本中使用一个模块,但我完全被阻止了。 I'd like to execute the following command or anything similar which allow my to make instances of MyClass我想执行以下命令或任何类似的命令来创建MyClass实例

from mypackage.mymodule import MyClass

Following databrick's documentation I have develop a python package with a single module locally as follows:按照databrick的文档,我在本地开发了一个带有单个模块的python包,如下所示:

mypackage
|- __init__.py
|- setup.py
|- mymodule.py

Then run python setup.py bdist_wheel obtaining a .whl file.然后运行python setup.py bdist_wheel获得一个.whl文件。 The directory ends up being该目录最终成为

mypackage
|- build
   |- ... whatever
|- src.egg-info
   |- ... whatever
|- dist
   |- src-0.1-py3-none-any.whl
|- __init__.py
|- setup.py
|- mymodule.py

From here I've uploaded the .whl file into the Workspace following the instructions.从这里我按照说明将.whl文件上传到工作区 But now I'm not able to import MyClass into any notebook.但现在我无法将MyClass导入任何笔记本。

I've tried all approches below:我已经尝试了以下所有方法:

  • upload the .whl with and without a name.上传带名称和不带名称的.whl
  • upload the .whl installing it into the cluster and not.上传.whl将其安装到集群中,而不是。
  • use import mypackage使用import mypackage
  • use dbutils.library.install('dbfs:/path/to/mypackage.whl/') (which returns True ) and then use import ...使用dbutils.library.install('dbfs:/path/to/mypackage.whl/') (返回True ),然后使用import ...
  • instead of uploading a .whl , create the package folder in the same directory as the notebook.而不是上传.whl ,而是在与笔记本相同的目录中创建包文件夹。
  • Upload to my folder and to Shared folder上传到我的文件夹和Shared文件夹
  • all combinations of the above.以上所有组合。 f.ex: uploading with different name and use import differentname f.ex:使用不同名称上传并使用import differentname

This is driving my crazy.这让我发疯了。 I its such a simple task which I can achive easily with regular notebooks.这是一项如此简单的任务,我可以使用普通笔记本轻松完成。

I've solved this by using python's egg instead of wheel .我已经通过使用 python 的egg而不是wheel解决了这个问题。 python setup.py bdist_egg will create an egg which you can install following databricks docs. python setup.py bdist_egg将创建一个鸡蛋,您可以按照 databricks 文档安装它。 I don't know why wheel doesn't work...我不知道为什么wheel不工作...

With the the introduction of support for arbitrary files in Databricks Repos , it is now possible to import custom modules/packages easily, if the module/package resides in the linked git repo.随着在 Databricks Repos 中引入对任意文件支持,如果模块/包驻留在链接的 git 存储库中,现在可以轻松import自定义模块/包。

First,第一的,

  1. Make sure Repos for Git integration is enabled.确保启用了用于 Git 集成的 Repos
  2. Make sure support for arbitrary files is enabled.确保启用了对任意文件的支持。

Both of these can be enabled from Settings -> Admin Console -> Workspace Settings.这两者都可以从设置 -> 管理控制台 -> 工作区设置中启用。

Then, with the following directory structure in the git repo,然后,在 git repo 中使用以下目录结构,

.
├── mypackage
│   ├── __init__.py
│   └── mymodule.py
└── test_notebook

it is possible to import the module mymodule in the package mypackage from test_notebook simply by executing the following statement:只需执行以下语句,就可以从test_notebook importmypackage的模块mymodule

# This is test_notebook in the above filetree
from mypackage.mymodule import MyClass

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

相关问题 如何在 Azure Databricks Notebook 中使用 pysft 模块 - How to use pysft module in Azure Databricks Notebook 如何在 Jupyter Notebook 中导入本地模块 - How to import a local module in Jupyter Notebook 如何将一个数据块笔记本导入另一个? - How to import one databricks notebook into another? 如何导入数据砖笔记本中轮文件中的方法? - How to import a method which is in wheel file in databricks notebook? 如何从 azure databricks 笔记本运行机器人框架脚本? - How to run a robot framework script from azure databricks notebook? Azure Databricks API,如何通过 API 将集群附加到上传的笔记本 - Azure Databricks API, how to attach a cluster to an uploaded notebook via API 如何在 Azure Databricks 笔记本中调试长时间运行的 python 命令? - how to debug long running python commands in Azure Databricks notebook? 如何正确地将 datetime 模块导入 jupyter notebook? - How to properly import datetime module into a jupyter notebook? 如何在 jupyter 笔记本中导入 python 类/模块? - How to import a python class/module in a jupyter notebook? 如何在 Jupyter Notebook 中将 python 文件作为模块导入? - How to import python file as module in Jupyter notebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM