'ModuleNotFoundError' in Thonny - what it is and how to fix it

PUBLISHED 22 Aug 2020

I have been learning Python since the end of last year, and recently moved from IDLE to Thonny. I was surprised to almost immediately hit a 'ModuleNotFoundError' in Thonny - here's why, and how to fix it.

IDLE is the Integrated Development Environment (or IDE) that comes bundled with the default implementation of Python, so I am sure that many new Pythonistas begin their Python journey using IDLE. I certainly did.

IDLE has the benefit of simplicity, but is perhaps best suited to absolute beginners. After a while I was ready to move to a different IDE and chose Thonny: still simple, still free, but with some useful features for learners such as the ability to debug step by step without adding breakpoints first.

However when I tried to run many of my Python scripts in Thonny I received a 'ModuleNotFoundError' like this:

Modulenotfounderror in Thonny

And it wasn't just pandas: none of the packages I had been using with IDLE, from seaborn to matplotlib, were available in Thonny! Running a script that required any of them threw a similar 'ModuleNotFoundError'. I'd installed these packages using pip (not via IDLE) so had assumed they would all be available for use in any IDE, Thonny included.

The reason for this confusion is that Thonny comes with Python 3.7 built in (or 'bundled'), and by default uses this Python installation rather than the one you may have previously been using with a different IDE. If you look at the first line of the screengrab above, you can see that Thonny is telling me that I am using "Python 3.7.7 (bundled)". For reference it is located at:

You will also see this Python installation referred to in Thonny's menus as "the same interpreter which runs Thonny".

If Thonny is your first experience of Python, then I suppose that bundling Python with Thonny removes the need to install Python separately and eliminates one barrier to entry - a good thing. However if you are moving to Thonny from a different IDE then it can be quite disconcerting!

If you find yourself in this situation, there are two things you can do. Perhaps the easier approach is to swap back to your previous Python interpreter (installation) - or you could install your required packages for use with Thonny's bundled interpreter. Here's how to do either:

Changing Python interpreter in Thonny

  1. In Thonny's menu, go to Run > Select interpreter
  2. In the 'Which interpreter or device should Thonny use for running your code?' dropdown, select Alternative Python 3 intepreter or virtual environment
  3. If yours isn't already in the list of 'known interpreters', select Locate another python.exe and navigate to your Python executable. Unless you've installed it somewhere unusual, it's likely to be something like the following:
  4. Click OK
  5. Now let's check that has done the trick. In Thonny's menu, go to Tools > Manage packages...
  6. Thonny's package manager will show you all installed packages in the panel on the left. Hopefully you will see everything you are expecting!
  7. Incidentally you can click on any package to see metadata such as version number, summary and author/s, and dependencies:

Thonny package managerFor example here you can see that pandas requires numpy among other packages, and that I don't have the latest stable version of pandas installed.

Installing packages for Thonny's default interpreter

  1. In Thonny's menu, go to Run > Interpreter
  2. In the 'Which interpreter or device should Thonny use for running your code?' dropdown, make sure The same interpreter which runs Thonny (default) is selected
  3. Hit OK
  4. In Thonny's menu, go to Thonny's package manager (Tools > Manage packages)
  5. Some packages will already be installed, eg the Python imaging library 'pillow' - you will see these listed in the pane on the left. However if you have previously installed packages such as matplotlib, pandas and numpy for use in IDLE or a different IDE, then they are likely to be missing from this list and will need to be installed for use with Thonny's default interpreter.
  6. Type your package name in the search field at the top (eg matplotlib) - you need the exact package name. If in doubt, check the Python Package Index at pypi.org
  7. Click Find package from PyPI
  8. Click Install
  9. Depending on the size of the package, the installation can take a little while - so wait until you get a success message and/or the popup disappears
  10. Once you have installed all the missing packages, click Close. They will be available for use immediately.

Although initially confusing, the approach that Thonny takes is pretty powerful. It enables you to set up and very quickly swap between multiple Python interpreters, and see at a glance which packages have been installed for use with each. This could help you, for example, avoid dependency conflicts.

Notify of
Inline Feedbacks
View all comments
Your Boy Chips Ahoy
Your Boy Chips Ahoy
3 years ago

you could also change the interpreter to the you actual python interpreter and not the thonny python one.

2 years ago

Hey! Thanks a million. Looked through many solutions - but this was the only REAL one where anyone knew what they were talking about. Everyone else either was teaching to install - pip install - the packages or were on a tangent about importing user defined scripts.

Once again - Thanks. (So that makes it a million and one thanks.:) ))

Albert Hartholt
Albert Hartholt
2 years ago

Thank you very much for your article. Also for me it worked!
I got the same problem with my Raspberry Pi. Two versions of Python are installed on it. Maybe is he reaswon that Thonny couldn't find it and I got always the ModuleNotFoundError: No module found named psycopg2.
I had to go in the Thonny's menu tot the package manager (Tools> Manage packages. just as you described, and find the package from PyPi. Then: click Install

Jura Kotoul
Jura Kotoul
2 years ago

Thanks, James!
You pointed to the right spot. Would be best to trigger Thonny authors to detect another Python installation at first run and let the user to choose...

J. F.
J. F.
1 year ago

I cannot thank you enough!!! I have spent HOURS tonight scouring the internet trying to find a solution and have wasted a ton of time uninstalling, reinstalling, trying pip install vs. pip3 install, etc., etc. I was ready to pull my hair out because I kept getting the error for several different modules in several different projects that are due for my class very soon. I agree with another comment that it would be incredibly helpful if Thonny detected another Python installation and alerted the user to select the default interpreter upon initial use, but at least now I know… Read more »

1 year ago

thanks a lot!!!

1 year ago

I forgot to comment in previous thanks...
i tryed to reinstal the module for thonny but pypi responded that the module was already instaled, evidently in the first python but not in thonny...
but i didnt knew/find how to change interpreter.
I even tryed to copy paste the modules in the thonny folder...
thanks again

1 year ago

i use pythonw.exe no python.exe ubication: C:\Users\yourUser\AppData\Local\Programs\Python\Python38

change "yourUser"
in windows configure interpreter, pres in the three points, the error in thony i s thas these could be a button but that see like only text, when apear tje windows for sheck the rutr press "all files(*.*)" and select pythonw.exe not the python.exe

Last edited 1 year ago by laParca
James Clark
Hi! I'm James Clark and I'm a freelance web analyst from the UK. I'm here to help with your analytics, ad operations, and SEO issues.
What do you think? Leave a commentx
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram