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.
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).