A common error you may encounter when using Python is modulenotfounderror: no module named ‘thread’.
This error occurs if you try to import the built-in module _thread using import thread
.
The thread
module was renamed to _thread in Python version 2.7+.
You can solve this error by importing the module with import _thread
.
The error can also occur if you tried to use the Thread class from the threading module. You can solve the error by importing the Thread
class from the threading
module. For example,
from threading import Thread
This tutorial goes through how to solve this error with code examples.
Table of contents
What is ModuleNotFoundError?
The ModuleNotFoundError occurs when the module you want to use is not present in your Python environment. There are several causes of the modulenotfounderror:
The module’s name is incorrect, in which case you have to check the name of the module you tried to import. Let’s try to import the re module with a double e to see what happens:
import ree
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) 1 import ree ModuleNotFoundError: No module named 'ree'
To solve this error, ensure the module name is correct. Let’s look at the revised code:
import re print(re.__version__)
2.2.1
You may want to import a local module file, but the module is not in the same directory. Let’s look at an example package with a script and a local module to import. Let’s look at the following steps to perform from your terminal:
mkdir example_package cd example_package mkdir folder_1 cd folder_1 vi module.py
Note that we use Vim to create the module.py
file in this example. You can use your preferred file editor, such as Emacs or Atom. In module.py
, we will import the re module and define a simple function that prints the re version:
import re def print_re_version(): print(re.__version__)
Close the module.py
, then complete the following commands from your terminal:
cd ../ vi script.py
Inside script.py
, we will try to import the module we created.
import module if __name__ == '__main__': mod.print_re_version()
Let’s run python script.py
from the terminal to see what happens:
Traceback (most recent call last): File "script.py", line 1, in ≺module≻ import module ModuleNotFoundError: No module named 'module'
To solve this error, we need to point to the correct path to module.py
, which is inside folder_1
. Let’s look at the revised code:
import folder_1.module as mod if __name__ == '__main__': mod.print_re_version()
When we run python script.py
, we will get the following result:
2.2.1
You can also get the error by overriding the official module you want to import by giving your module the same name.
Lastly, you can encounter the modulenotfounderror when you import a module that is not installed in your Python environment.
In this example, _thread
is a built-in module, which means it comes automatically with Python.
The thread module was renamed to _thread in Python version 2.7+. Therefore, if you try to use the command import thread
with Python 3, you will raise the ModuleNoteFoundError: No module named ‘thread’.
What is _thread?
The _thread module provides low-level primitives for working with multiple threads.
Example
Let’s look at an example to reproduce the error.
import thread lock = thread.allocate_lock() with lock: print("lock is locked while process runs")
Let’s run the code to see what happens:
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Input In [2], in <cell line: 1>() ----> 1 import thread 3 lock = thread.allocate_lock() 5 with lock: ModuleNotFoundError: No module named 'thread'
The error occurs because thread
is not the correct spelling for the built-in _thread
module.
Solution
We can solve this error by using the correct name for the _thread
module.
Then we can access the allocate_lock()
method from the module. Let’s look at the revised code:
import _thread lock = _thread.allocate_lock() with lock: print("lock is locked while process runs")
Let’s run the code to get the result:
lock is locked while process runs
We can also use the from
keyword to import the allocate_lock()
method directly at the top of the script. Let’s look at the revised code:
from _thread import allocate_lock lock = allocate_lock() with lock: print("lock is locked while process runs")
Let’s run the code to see the result:
lock is locked while process runs
We can write a try/except statement to import _thread if Python 3 is in use otherwise thread if Python 2 is in use.
# Python 2.7+ Example import sys print(sys.version) try: # For Python 2.7+ import _thread as thread print('Importing _thread for Python 2.7+') except ModuleNotFoundError: # Attempt to import thread for Python 2.6 and earlier import thread print('Importing thread for Python 2.6 and earlier') lock = thread.allocate_lock() with lock: print("lock is locked while this runs")
Let’s run the code to see the result:
3.8.8 (default, Apr 13 2021, 12:59:45) [Clang 10.0.0 ] Importing _thread for Python 3 lock is locked while this runs
Summary
Congratulations on reading to the end of this tutorial.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
For further reading on missing modules in Python, go to the article:
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.