The ModuleNotFoundError is an error in Python when a module is not found. It usually appears when the user tries to import a module or package that does not exist or has been incorrectly written.

The error message will say something like “No module named ””. This type of error can occur in a few different scenarios.

  • First, it can occur when the user is trying to import a module that does not exist in their environment.
  • Second, it can occur if the user has written an incorrect name for the module or package they are trying to import.
  • Finally, it can also happen if there are permissions issues with the folder containing the module/package causing the import to fail.

Ultimately, resolving a ModuleNotFoundError: no module named Python Error is straightforward if the proper steps are taken.
By ensuring that the module or package exists in the environment and is correctly named and using a debugging tool to identify any issues, the user should be able to resolve this error and continue working without further interruptions effectively.

I tried to import the numpys module, but I got this error message:

				
					import numpys as np
				
			

Result:

				
					ModuleNotFoundError: No module named 'numpys'
				
			

Reasons a module may not be found:

  • If you use the incorrect casing for a module, it will still link back to the first point. So, for example, if you spell numpy as NumPy during import, it throws the module not found error because both modules are “not the same.”
  • When you misspell a module name, it links back to the previous point mentioned. For example, if you were to import the NumPy library but spelled it as numpys
  • The module you are attempting to import is not installed on your computer
  • You are attempting to import a module using an incorrect file path.

How to fix the ModuleNotFoundError

Below are a couple of solutions if you’re having trouble finding a module.

1. Ensure that all modules you plan to import are installed

Let’s use numpy as an example. You’d import this module into your code in a file called “test.py.” 

Example:

				
					import numpy as np

arr = np.array([1, 2, 3])

print(arr)
				
			

If you try to run this code with python test.py and you get this error:

				
					ModuleNotFoundError: No module named "numpy"
				
			

If you’re getting an error that the NumPy module is not installed, don’t worry – it’s easy to fix. Just run the following command:

				
					python -m pip install numpy
				
			

After you install the code below, it will function properly and give you the desired outcome in your terminal.

				
					[1, 2, 3]
				
			

2. Make sure modules are in the right casing

You might be spelling the module correctly but using the wrong casing. 

Example:

				
					import Numpy as np

arr = np.array([1, 2, 3])

print(arr)
				
			

Even numpy is installed and the code below will generate an error:

				
					ModuleNotFoundError: No module named 'Numpy'
				
			

Because NumPy and Numpyare wrote differently, they refer to different modules. To fix this error, could you make sure to spell the module name correctly.

3. Use absolute imports

After understanding how the import statement is executed and the difference between absolute and relative imports, we can discuss what to do when your application experiences errors like ModuleNotFoundError or ImportError.

The two most common errors occur when cannot find the module’s name in the sys.path Remember that if you try to import a module and it’s not in sys.modules or the standard library, Python will look for it in sys.path . Similarly, if you use the syntax (e.g. from mypackage import a ), Python will first try to load the module. If it can’t find the module, Python will throw ModuleNotFoundError for imports or ImportError for attempted loads.

				
					└── myproject
    ├── mypackage
    │   ├── a.py
    └── anotherpackage
        ├── b.py
        ├── c.py
        └── mysubpackage
            └── d.py
				
			
  • first, make sure you are using absolute imports
  • export the project’s root directory to PYTHONPATH

If your Python IDE doesn’t automatically do this, you can probably find an option to manually set the PYTHONPATH for your application.

If you’re running your application in any other environment such as Docker, Vagrant, or virtually, you can use the below command:

				
					export PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"
# * For Windows
set PYTHONPATH=%PYTHONPATH%;C:\path\to\your\project\
				
			

By appending your project’s root directory to PYTHONPATH, your absolute imports should now work as intended.

Also, you can try appending /path/to/your/project/ to sys.path; however, this is not advised, unfortunately.

Wrap up

When Python attempts to locate an imported module and cannot find it in the built-in library, installed modules, or current project directory, it throws a ModuleNotFoundError. As a result, you may encounter an error when installing a module that is not installed, is misspelled, has the wrong casing, or is in the wrong directory. In this article, I will show four possible ways to fix this issue.


Thanks for reading. Happy coding!