在Python中导入自定义模块的问题
有一个奇怪的问题。我有两个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论