Reputation: 9
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. What's the reason behind it?
Can someone explain me why file name shouldn't be same as module name?
Upvotes: 0
Views: 852
Reputation: 27577
When you import a module, python will search for that module, which is in the from of a python file. Here is how the searching is done: How does python find a module file if the import statement only contains the filename?
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!
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.
Upvotes: 1