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.
from multiprocessing import Process
def f(name):
print 'hello', name
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
When I ran this file, I recieved the following error
Traceback (most recent call last):
File "multiprocessing.py", line 1, in <module>
from multiprocessing import Process
File "C:\Dev\multiprocessing.py", line 1, in <module>
from multiprocessing import Process
ImportError: cannot import name Process
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
import multiprocessing
if __name__ == '__main__':
print 'Location:', multiprocessing.__file__
The output of this cleared up the problem like day & night
Location c:\Dev\multiprocessing.py
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.