-->
With Examples Fix: ModuleNotFoundError: No module named 'module' and ImportError: attempted relative import with no known parent package Python

With Examples Fix: ModuleNotFoundError: No module named 'module' and ImportError: attempted relative import with no known parent package Python

Back to top

Updated by Ashirafu Kibalama on September 17, 2024

Why do you see "ModuleNotFoundError: No module named 'module' and ImportError: attempted relative import with no known parent package Python" and How to Fix them?





Facing ModuleNotFoundError: No module named 'module' or ImportError: attempted relative import with no known parent package in Python?


The ModuleNotFoundError: No module named 'module' occurs when Python can't locate the specified module. Correct the module name and ensure it's in your Python path.


The ImportError: attempted relative import with no known parent package Python occurs with improper relative imports—switch to absolute imports to run your code as part of a package. Check out our guide for detailed examples and solutions!


Fix or Resolve: ModuleNotFoundError: No module named 'module' Python With Examples.




The ModuleNotFoundError: No module named 'module' in Python occurs when Python can't find the specified module you're trying to import.


# my project:





#main.py

from unittest.mock import patch

# Correctly use 'patch' as a context manager or decorator
with patch('module.function_name') as mock_func:
mock_func.return_value = "mocked result"



# modules.py





To fix it:

1) Rename 'module

Rename 'module' with the actual name of the module you're working with.



# Correctly use 'patch' as a context manager or decorator
with patch('module.function_name') as mock_func:


My module name is modules.py, but I use 'module' in the above line, which raises the ModuleNotFoundError: No module named 'module'. So, make changes as required.


So either change modules.py



to module.py:




Or change module to modules within the main.py:


 



2) Module Availability

Ensure the module is available in your environment and correctly spelt. If it's part of your project, ensure it's located in the correct directory or added to your Python path.


3) Relative Imports:

If you're using relative imports, ensure they are correctly formatted.

For example:



from .forms import my_modules



Fix or Resolve ImportError: attempted relative import with no known parent package Python With Examples:




The ImportError: attempted relative import with no known parent package in Python occurs when you use relative imports outside of a package or run a module as a script.


To fix it:

1) convert relative imports to absolute imports to ensure you run your code within a package context.


Code example before fixing the ImportError: attempted relative import with no known parent package Python:


from . import module

module.function_name()


Output:




Code example: After fixing the ImportError: attempted relative import with no known parent package Python:



# from . import module
import module # change from . import module to import module

module.function_name()


or



# from . import module
from module import function_name # change from . import module to from module import function_name

function_name()



Output:






Conclusion

Understanding the root cause of ModuleNotFoundError: No module named 'module' and ImportError: attempted relative import with no known parent package Python:

  • whether missing module
  • or misnamed module
  • or the misuse of relative imports—allows you to correct your code effectively.


Use absolute imports and correctly name your modules to run your scripts in the proper context.


Other Posts:

1 With Examples Fix: Python Object Has No Attribute, but it Does.


2 With Examples Fix attributeerror: 'function' object has no attribute func and AttributeError: 'function' object has no attribute assert_called Python.


3 With Examples Fix: AttributeError: 'function' object has no attribute 'patch and Attributeerror: 'function' object has no attribute count Python


4 With Examples Fix: attributeerror: 'function' object has no attribute register and AttributeError: 'function' object has no attribute 'get Python


5  With Examples fix: AttributeError: 'function' object has no attribute 'glob and AttributeError: 'function' object has no attribute 'get_extra_actions Python


6 How to Fix AttributeError: 'dict_values' object has no attribute 'update_relative' in Python With Examples.