0

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?

Ann Zen
  • 17,892
  • 6
  • 20
  • 39
  • 1
    The file name, minus the `.py` extension, *is* the module name. I have no idea what you're actually asking here; you need to be more specific and detailed, ideally with a [MCVE] describing what you tried, what you expected, and what went wrong (including traceback if applicable). – ShadowRanger Dec 22 '20 at 02:38

1 Answers1

0

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 AttributeErrors, thrown at them when running their code, and come asking about it on Stack Overflow.

Ann Zen
  • 17,892
  • 6
  • 20
  • 39
  • @Sangam Chhetri And these errors are very hard to be recognised. An example is this question I asked once. https://stackoverflow.com/questions/65167161/class-lifoqueuequeue-queue-attributeerror-module-queue-has-no-attribute-q – Shadowcoder Dec 22 '20 at 03:20
  • @Shadowcoder I went ahead an provided an answer, as comments are temporary. – Ann Zen Dec 22 '20 at 13:07