Python Module Naming Pitfall

I recently ended up naming my Python .py module as the same name as one of the standard Python library modules. This caused an import exception and ended up wasting me a lot of time and left me feeling completely lost for a while trying to debug it. Here is what I did and how I was eventually able to figure it out. I hope this helps someone else that may fall into the same pitfall!

I was trying to learn the standard Python multiprocessing module. I took the first example on the docs for multiprocessing module and put it in a file called multiprocessing.py.

When I ran this file, I recieved the following error

I was using Windows, hence I thought maybe my current setup does not support multiprocessing. Once I confirmed that it should be supported, I wasted time trying to confirm my standard package paths on my system. Eventually I gave up and searched online to find the following tip on StackOverflow.

The tip suggested that I try the following bit of code, using the __file__ property of a module to see where it’s being loading from

The output of this cleared up the problem like day & night

Hence, my own module was being loaded as the multiprocessing module. I could not have imagined that this was possible. I didn’t think that a running module can actually import itself, but that was indeed the problem. Once I renamed my module to multiproc.py, everything worked properly.