Software⏱️ 2 min read📅 2026-05-30

How to Fix: How to fix "Attempted relative import in non-package" even with __init__.py

Learn how to fix: How to fix "Attempted relative import in non-package" even with __init__.py.

Quick Answer: Try checking your system settings or restarting.

The 'Attempted relative import in non-package' error occurs when you try to use a relative import in a file that is not part of a package. To fix this issue, you need to modify the import statement in your test file to use an absolute import or a relative import within a package.

🔍 Why This Happens

  • [Cause]

🛠️ Step-by-Step Verified Fixes

Method 1: Absolute Import

  1. Step 1: Modify the import statement in core_test.py to use an absolute import, like this:

Example:

from pkg.components.core import GameLoopEvents

Method 2: Relative Import within Package

  1. Step 1: Add a relative import to the __init__.py file in the components directory, like this:

Example:

from .core import GameLoopEvents

✨ Wrapping Up

By following these steps, you should be able to resolve the 'Attempted relative import in non-package' error and get your project up and running.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions