This Article discusses the famed “no module named xxx” import error in Python.
As someone who owns a Coding website and blog, I tend to keep an eye out on the Programming community and their troubles. If I notice enough demand for a certain topic or question, I’ll create a tutorial or make a post about it.
One day while going through a list of popular googles searches, I happened to notice an unusually large number of people making searches like “no module name xxx“. For some Python libraries, there would be over 10k monthly searches like this. If one were to add them all up, they would probably number well over 100k per month.
At any rate, I was quite surprised, and have decided to make this article as a guide, explaining this error, how to fix it and how to avoid running into this in the future.
What does this Error mean?
As the name implies, this error occurs when Python is unable to locate a module that you have tried importing. And as expected of the Python interpreter, it immediately returns an exception,
ModuleNotFoundError and shuts the program down.
So why was Python unable to find this module? Let’s assume you were trying to import this library called fly. You used the
import fly statement, but of course, since no module called fly exists, the following error was thrown.
Traceback (most recent call last):
File "C:\Users\CodersLegacy\Desktop\script.py", line 1, in <module>
ModuleNotFoundError: No module named 'fly'
We’ll now proceed to examine every possible scenario in which this error can occur and how to fix it.
Library not installed
This is by far the most common reason. You’ve tried to import a library that you saw someone use in a YouTube tutorial, but the problem here is that Python does not come with all these libraries pre-installed and downloaded. Only a certain number of libraries, known as the Standard library can be imported directly. Popular examples of such libraries are DateTime and Tkinter.
So what do you do now? Well, you head over to the command prompt, navigate to your Python installation and use the Python package installer pip to download the library of your choice. It’s a very simple command,
pip install <insert library here>.
Since I have the path to my
Python.exe added to the list of default paths, I don’t have to manually navigate to it. If you were confused about any step in this process, head over to the Python setup guide where it’s discussed in detail.
Making sure you’re in the correct directory is of utmost importance. Most people make this mistake and simply try to call the
pip install from the default location on the command prompt. You must first navigate to the folder with your
Python.exe and then call the command. If you don’t know how to navigate the command prompt, you’ll have to look it up.
Multiple Python Installations
One issue that can complicate things is if you have multiple Python installations on your computer. And this is easily possible. As I’m writing this, I currently have two Python installations myself (accidentally of course). The first I happened to download when I was setting up Visual Studio, which would serve as my IDE for Python for a good while. My Python.exe file path looks something like this.
C://Program Files/Microsoft Visual Studio/Python37
Now, every time I download a library, it’s installed in the above folder. As a result only the
python.exe in this folder can access those libraries. The second Python installation of mine was done through the regular method, and is stored elsewhere. As a result, it cannot access the libraries installed with the first Python installation.
Now how is this relevant to this article? Well, if you have multiple python installations, there’s a good chance you’ve gotten confused between the two and are importing the library from the wrong place. If this is the case, before rushing over to google this error, make sure that the library is installed at the Python installation you’re currently using.
Incorrect Library name
While extremely unlikely, there is a chance you messed up the library names. I know for fact that sometimes the actual name of a library and the name used to install the library can be a bit different.
For instance the popular web scrapper library BeautifulSoup is installed and imported with the name
bs4. Some people may try to download or import it using the name BeautifulSoup, and that would return an error.
There’s a simple to find the name through which a library is imported. Do a google search like Python numpy and click on the first tutorial for it. 9 times out of 10 there will be a small section at the top where they mention how to install the library.
Importing a file
Unbeknownst to many python programmers, you can actually import another python file into your python program. Why we do this is a story for another article, where it’s explained in greater detail. But for now, we’ll just cover the import problems that may be caused while doing this.
Let’s assume you’re trying to import a file called
data.py. To import this correctly, you must use
import data. Note the lack of an extension. Secondly, it must be in the same directory as the file you’re importing it into. If the Python files are not in the same directory, a no module named data error will pop up.
You can still import a file even if it’s in another directory, but the process is a bit shaky, so its best to avoid it.
One final tip. Be careful while naming your files. Do not use the names of any libraries, like
csv.py. If you do so, and simultaneously import that same library, Python will get confused between the two and try importing the file instead of the library.
I think that just about covers every scenario in which you could possibly face this error. Hopefully you’ve learnt something from this article and won’t have to worry about this error in the future.
BONUS: If you are interested in learning more about importing modules in Python, check out this tutorial which teaches you how to import a module using a user-input string.
This marks the end of the Python “no module named” error Article. Any suggestions or contributions for CodersLegacy are more than welcome. Relevant questions regarding the article can be asking in the comments section below.