Ausgabe
import threading
import time
start = time.perf_counter()
def do_something():
print("Sleeping in 1 second")
time.sleep(1)
print("Done sleeping")
t1 = threading.Thread(target=do_something)
t2 = threading.Thread(target=do_something)
finish = time.perf_counter()
print(f"Finished in {round(finish-start,1)} seconds(s) ")
Weiß jemand, warum dieser Codeabschnitt diesen Fehler zurückgibt, wenn er ausgeführt wird, und wie er behoben werden kann?
Traceback (most recent call last):
File "c:/Users/amanm/Desktop/Python/Python Crash Course/threading.py", line 1, in <module>
import threading
File "c:\Users\amanm\Desktop\Python\Python Crash Course\threading.py", line 12, in <module>
t1 = threading.Thread(target=do_something)
AttributeError: partially initialized module 'threading' has no attribute 'Thread' (most likely due to a circular import)
Wenn ich diesen Code im normalen IDLE ausführe, scheint er zu funktionieren, aber er funktioniert nicht in Visual Studio Code.
Lösung
Es sieht so aus, als ob die von Ihnen erstellte Programmdatei den Namen threading.py
hat und Sie ein Modul importieren, das auch threading
. Dies führt zu einem Zirkelimport, da Ihre Datei das integrierte Modul schattiert.
Bitte benennen Sie Ihr Programm um (zB threading-example.py
).
Beantwortet von – Lenka Čížková
Antwort geprüft von – Dawn Plyler (FixError Volunteer)