如何将模块导入Databricks笔记本中的另一个模块?

发布于 2025-01-20 21:31:33 字数 380 浏览 1 评论 0原文

这是我在 Databricks 中的 config.py

DATA_S3_LOCATION='s3://server-data/data1'
DATA_S3_FILE_TYPE='orc'
DATA2_S3_LOCATION='s3://server-data/data2'
DATA2_S3_FILE_TYPE='orc'

我有 init 。 py 也在这个文件夹中

我试图访问另一个文件中的这些变量

import sys
sys.path.insert(1,'/Users/file')
from file import config

我遇到错误,没有名为文件的模块

This is my config.py in Databricks

DATA_S3_LOCATION='s3://server-data/data1'
DATA_S3_FILE_TYPE='orc'
DATA2_S3_LOCATION='s3://server-data/data2'
DATA2_S3_FILE_TYPE='orc'

I have init . py in this folder as well

I am trying to access these variables in another file

import sys
sys.path.insert(1,'/Users/file')
from file import config

I am facing error , no module named file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-01-27 21:31:33

这里有几个方面。

  • 如果这些文件是笔记本,则需要使用 %run ./config 包含当前目录中的笔记本 (doc)
  • 如果您使用 数据块启用了 Repos 和任意文件支持,那么您的代码需要是 Python 文件,而不是笔记本,并且具有 __init__.py 等正确的目录布局。在这种情况下,您可以使用 Python 导入。您的存储库目录将自动添加到 sys.path 中,您无需修改​​它。

PS 我有一个 示例 存储库,其中包含笔记本和笔记本电脑Python 文件方法。

There are several aspects here.

  • If these files are notebooks, then you need to use %run ./config to include notebook from the current directory (doc)
  • if you're using Databricks Repos and arbitrary files support is enabled, then your code needs to be a Python file, not notebook, and have correct directory layout with __init__.py, etc. In this case, you can use Python imports. Your repository directory will be automatically added into a sys.path and you don't need to modify it.

P.S. I have an example of repository with both notebooks & Python files approaches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文