在Python中导入自定义模块的问题

发布于 2025-02-08 05:39:32 字数 761 浏览 1 评论 0原文

有一个奇怪的问题。我有两个jupyter笔记本。其中一个位于与我要导入的模块相同的目录中。另一个笔记本 - 在另一个DIR中。至于同一DIR中的笔记本,一切都很好。但是与他人一起工作会带来很多麻烦。

好吧,导入首先使用Sys.Append(DIR)将软件包添加到PythonPath。之后,可以轻松地导入此 main_module.py 。 但是,开始使用它,我会遇到错误。主模块使用另一个。根看起来像:

  • dir
    • main_module.py
    • folder1
      • Tech.py​​
    • folder2
      • conf.ini

main_module.py 使用 tech.py​​.py 的功能,而Tech.py​​从 conf.ini 获得信誉。在DIR中的笔记本中使用main_module.py时,可以正常工作,但是从另一个dir(添加sys.path.append(path_to_dir) conf.ini )无法解析,我抓住了KeyError。这意味着Tech.py​​不会“看到” conf.ini。

通往conf.ini的方式看起来像./ folder2/conf.ini

There is a strange problem. I have two Jupyter Notebooks. One of them is located in the same directory as the module I'm going to import. Another notebook - in another dir. As for the notebook in the same dir, everything works pretty good. But working with another makes lots of troubles.

Well, the import starts with adding the package to PYTHONPATH using sys.append(dir). After this main_module.py can be imported easily.
However, starting using it I get a error. Main module uses another one. The root looks like:

  • dir
    • main_module.py
    • folder1
      • tech.py
    • folder2
      • conf.ini

The main_module.py uses functions from the tech.py, whereas tech.py get creds from conf.ini. While using main_module.py from the notebook in the dir, that works correctly, but from another dir (with adding sys.path.append(path_to_dir)) the conf.ini can't be parsed and I catch KeyError. This means that the tech.py doesn't "see" the conf.ini.

The way to conf.ini looks like ./folder2/conf.ini.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文