ModuleNotFoundError: No module named 'thread' in Python

avatar

Borislav Hadzhiev

Last updated: Apr 18, 2022

banner

Check out my new book

ModuleNotFoundError: No module named 'thread' in Python #

The Python "ModuleNotFoundError: No module named 'thread'" occurs when we incorrectly import the _thread module or shadow it with a thread.py file. To solve the error, import the module as import _thread command.

no module named thread

main.py
import _thread a_lock = _thread.allocate_lock() with a_lock: print("a_lock is locked while this runs")

The thread module was renamed to _thread in Python v3.

If you need a universal import that works for both Python 2 and 3, use a try/except statement.

main.py
try: import _thread as thread # using Python 3 except ImportError: import thread # falls back to import from Python 2 a_lock = thread.allocate_lock() with a_lock: print("a_lock is locked while this runs")

We try to import the _thread module (Python 3) and if we get an ImportError, we know the file is being ran in Python 2, so we import thread instead.

We aliased the _thread module to thread for consistency.

If you meant to use the threading module import threading instead.

main.py
import threading print(threading.Thread)

The Python error "ModuleNotFoundError: No module named 'thread'" occurs for multiple reasons:

  1. Using an incorrect import statement when importing the module.
  2. Naming your module thread.py which would shadow the official module.
  3. Declaring a variable named thread which would shadow the imported variable.

If you aren't sure what version of Python you're using, run the python --version command.

shell
python --version

get python version

If you are in a virtual environment, the version of Python corresponds to the version that was used to create the virtual environment.

If you have multiple versions of Python installed on your machine, your IDE might be setup to use an incorrect version.

For example, In VSCode, you can press CTRL + Shift + P or ( + Shift + P on Mac) to open the command palette.

Then type "Python select interpreter" in the field.

python select interpreter

Then select the correct python version from the drop down menu.

select correct python version

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

If you aren't sure whether your virtual environment is created using the correct Python version, try recreating it or creating a new virtual environment if you don't have one.

The _thread module is from the standard library so it will be available in a virtual environment.

shell
# 👇️ deactivate deactivate # 👇️ remove the old virtual environment folder rm -rf venv # 👇️ specify correct Python version python3 -m venv venv # 👇️ activate on Unix or MacOS source venv/bin/activate # 👇️ activate on Windows (cmd.exe) venv\Scripts\activate.bat # 👇️ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # 👇️ install the modules in your requirements.txt file pip install -r requirements.txt

Your virtual environment will use the version of Python that was used to create it.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.