[英]Why shouldn't I name my file the name of a module?
I mistakenly wrote the file name as one of the module in the python but most people suggest that file name should not be same as module name.我错误地将文件名写为 python 中的模块之一,但大多数人建议文件名不应与模块名相同。 What's the reason behind it?背后的原因是什么?
Can someone explain me why file name shouldn't be same as module name?有人可以解释一下为什么文件名不应该与模块名相同吗?
When you import a module, python will search for that module, which is in the from of a python file.当您导入一个模块时,python 将在 python 文件的 from 中搜索该模块。 Here is how the searching is done: How does python find a module file if the import statement only contains the filename?以下是搜索的完成方式:如果 import 语句仅包含文件名,python 如何找到模块文件?
If you name your file the name of a module, if the file is in the way of python's searching path, it will think that the file you created is the module that you are trying to import, and import it instead!如果你把你的文件命名为一个模块的名字,如果文件在python的搜索路径中,它会认为你创建的文件是你要导入的模块,而是导入它!
This has, unfortunately, caused a great amount of users confused with the errors, namely AttributeError
s, thrown at them when running their code, and come asking about it on Stack Overflow.不幸的是,这导致大量用户对运行代码时抛出的错误(即AttributeError
)感到困惑,并在 Stack Overflow 上询问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.