Why do I get an “attempted relative import with no known parent package” error in Python 3?

Hey @akanshasrivastava.1121 and @joe-elmoufak

This error comes up because Python’s relative imports only work if the module is part of a recognized package, and the script is run as a module, not a standalone file.

I always recommend structuring your project as packages and using python -m to run modules.

For example:

python -m mypackage.mymodule

This tells Python about the parent package and avoids “attempted relative import with no known parent package.” Otherwise, the interpreter won’t know where the relative import fits