Python ModuleNotFoundError: No Module Named ‘Thread’

5/5 - (1 vote)

Python raises the ImportError: No module named 'thread' when it cannot find the library thread.

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:

import thread

Run this one:

import _thread

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 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 with the following code to create an alias for the module _thread. Because you named the alias, you can run import thread without ModuleNotFoundError.

from _thread import *
__all__ = ("error", "LockType", "start_new_thread", "interrupt_main", "exit", "allocate_lock", "get_ident", "stack_size", "acquire", "release", "locked")

Source: linusg

Understanding the “import” Statement

import thread

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 import statement. 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 ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

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)

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

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

Related Video

The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError:

How to Call a Function from Another File in Python?