Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name Thread

It's my first time to learn python, and I went ahead to try threading from this blog post. The problem is it seems to be outdated:

import time
from threading import Thread

def myfunc(i):
    print ("sleeping 5 sec from thread %d" % i)
    time.sleep(5)
    print ("finished sleeping from thread %d" % i)

for i in range(10):
    t = Thread(target=myfunc, args=(i,))
    t.start()

How do I run this code without having this error:

$ python helloworld.py
Traceback (most recent call last):
  File "helloworld.py", line 1, in <module>
    import threading
  File "c:\Documents and Settings\Hermione\learningPython\threading.py", line 2, in <module>
    from threading import Thread
ImportError: cannot import name Thread

It's also weird that I have threading.py there for the fact that I already deleted that file (and it keeps appearing!!), and a mysterious __PyCache__ folder.

like image 604
Jürgen Paul Avatar asked Aug 21 '12 09:08

Jürgen Paul


2 Answers

Your problem is that you once had a file called threading.py, which probably left a byte-code file called threading.pyc.

You have to delete it also.

Similiar question here.

EDIT: Realising that you are using python3, delete the __pycache__ directory as well (this is where the file resides using python3.2 and later).

like image 78
sloth Avatar answered Sep 21 '22 09:09

sloth


You've named your file threading.py which hides the standard library module by the same name. .. don't do that :-)

like image 23
thebjorn Avatar answered Sep 21 '22 09:09

thebjorn