First time here? Check out the FAQ!

Revision history  [back]

This error occurs when you try to import a module using a relative import, but Python cannot find the module's parent package.

To fix this error attempted relative import with no known parent package, you need to make sure that the module being imported is part of a package, and that the package is on the Python path. You can do this by adding an empty __init__.py file to the package's directory, and by making sure that the directory containing the package is on the Python path.

Alternatively, you can run the module using the -m flag with the Python interpreter, like this:

python -m mypackage.mymodule

This tells Python to run mymodule.py as a module, which will allow it to correctly resolve relative imports.

The main advantage of using relative imports is that they make it easier to move packages and modules around within a project, without having to update all the import statements. They also make it easier to reuse code across multiple projects, because the import paths can be modified to match the package hierarchy of each project.