简体   繁体   English

为什么我不应该将我的文件命名为模块的名称?

[英]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.

相关问题 为什么我会收到未定义但未包含在代码中的名称错误? - Why am I receiving a name error for a name not defined that I haven't included in my code? 为什么我的代码不应该重复? - Why is my code repeating when it shouldn't? 为什么我不应该将virtualenv推向Heroku? - Why shouldn't I push a virtualenv to Heroku? 为什么我无法使用os.listdir更改Python程序中文件的名称 - Why I can't change the name of my files in my Python program using os.listdir 为什么在我的 Windows 机器中,我必须将命令写为“py -m pip install ”module-name“ 而不是只写”pip install “module-name”? - Why in my Windows machine I have to write command as “py -m pip install ”module-name“ instead of just writing ”pip install “module-name”? 当__name__不等于'__main__'时该怎么办 - What to do when the __name__ shouldn't equal '__main__' 我不知道我的数据库名称 - I don't know my Database name 为什么我无法从 tkinter 获取条目值作为文件名...我正在尝试使用给定名称保存图像 - Why I can't get entry value from tkinter as a file name…I'm trying to save image with a given name 导入:模块名称与文件名相同 - Import: module name same with file name 我应该如何在Python中命名全局模块? - What should I name my global module in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM