Python raises the
ImportError: No module named 'thread' when it cannot find the library
The most common reason for Python’s
ModuleNotFoundError: No module named 'thread' is that you import it using
import thread. Fix the error by using
import _thread instead because the
thread module was renamed to
_thread in Python 3.
So, instead of running the following import statement in Python 3:
Run this one:
So, the following shows how the first does not work but the second works on my Windows computer with standard Python installation:
>>> import thread Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> import thread ModuleNotFoundError: No module named 'thread' >>> import _thread >>>
Note that there is no need to install the library
_thread because it is already part of the Python standard library:
Alternatively, the error may occur if you shadow the module with a
thread.py file or you really cannot change the
import thread statement.
To fix this scenario, you can go to you
site-packages folder and create a new file
thread.py with the following code to create an alias for the module
_thread. Because you named the alias
thread.py, you can run
import thread without
from _thread import * __all__ = ("error", "LockType", "start_new_thread", "interrupt_main", "exit", "allocate_lock", "get_ident", "stack_size", "acquire", "release", "locked")
Understanding the “import” Statement
In Python, the
import statement serves two main purposes:
- Search the module by its name, load it, and initialize it.
- Define a name in the local namespace within the scope of the
importstatement. This local name is then used to reference the accessed module throughout the code.
What’s the Difference Between ImportError and ModuleNotFoundError?
What’s the difference between
Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the
ModuleNotFoundError is a subclass of the
You can see this in this screenshot from the docs:
You can also check this relationship using the
issubclass() built-in function:
>>> issubclass(ModuleNotFoundError, ImportError) True
Specifically, Python raises the
ModuleNotFoundError if the module (e.g.,
thread) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an
If an import statement cannot import a module, it raises an
ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a
The following video shows you how to import a function from another folder—doing it the wrong way often results in the
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.