简体   繁体   English

你可以在 python 中同时使用相对和绝对导入吗?

[英]Can you use relative and absolute imports at the same time with python?

I am trying to make a modular framework but I'm having problems with some of the import statements.我正在尝试制作一个模块化框架,但我遇到了一些导入语句的问题。

On their own, the files that are in the framework function fine, but when I create the package and import that to another file, the imports made in the individual files throw an error that the module cannot be found.就其本身而言,框架中的文件运行良好,但是当我创建包并将其导入另一个文件时,在单个文件中进行的导入会引发无法找到模块的错误。

If I use relative imports for the package, can the files inside the package use absolute imports?如果我对包使用相对导入,包内的文件可以使用绝对导入吗? I've never had problems importing things like pandas, but it is throwing an error that pandas cannot be found.我在导入熊猫之类的东西时从来没有遇到过问题,但是它抛出了一个找不到熊猫的错误​​。

You can use them mixed up but that is discouraged of course.您可以混合使用它们,但当然不鼓励这样做。 The best is to always use absolute imports.最好是始终使用绝对导入。 Read more about that in the PEP8 docs: https://www.python.org/dev/peps/pep-0008/#imports在 PEP8 文档中阅读更多相关信息: https ://www.python.org/dev/peps/pep-0008/#imports

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM