ModuleNotFoundError: No module named 'Queue' in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

ModuleNotFoundError: No module named 'Queue' in Python #

The Python "ModuleNotFoundError: No module named 'Queue'" occurs when we import the queue module incorrectly or shadow it with a queue.py file. To solve the error, import the module as import queue and don't name your files queue.py.

no module named queue

Here is how you would import and use the queue module.

main.py
import threading # 👇️ make sure to import with lowercase q import queue # 👇️ assign to variable q = queue.Queue() def worker(): while True: item = q.get() print(f'Working on {item}') print(f'Finished {item}') q.task_done() # Turn-on the worker thread. threading.Thread(target=worker, daemon=True).start() # Send 15 task requests to the worker. for item in range(15): q.put(item) # Block until all tasks are done. q.join() print('All work completed')

Note that we imported queue with lowercase q. The module uses an uppercase Q in Python 2.

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

  1. Using an incorrect import statement when importing the module (e.g. uppercase Q).
  2. Naming your module queue.py which would shadow the official module.
  3. Declaring a variable named queue which would shadow the imported variable.

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

main.py
try: import queue # using Python 3 except ImportError: import Queue as queue # falls back to import from Python 2

We try to import the queue module (Python 3) and if we get an ImportError, we know the file is being ran in Python 2, so we import using an uppercase Q and alias the import to queue.

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.

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.