I’m encountering an error when running script.py
in VSCode using Ctrl+F5
, which results in the following traceback:
Traceback (most recent call last):
File "c:UsersAyubDesktopreplai-dataset-genis_useful_chatscript.py", line 1, in <module>
from ..utils import OpenAIWrapper
ImportError: attempted relative import with no known parent package
My project structure is as follows:
└── ????sample
└── ????.venv
└── ????main
└── script.py
└── __init__.py
└── utils.py
└── __init__.py
Both __init__.py
are empty to mark the directories as packages.
Here’s the content of utils.py
:
def say_hi():
print("Hello World")
And here’s script.py
:
from ..utils import say_hi
say_hi()
You can find the source code at: GitHub Repository.
Why does it occur here, and how can I resolve it?
I attempted to resolve the ImportError
by converting directories into packages with empty __init__.py
files, expecting this to enable successful relative imports within my project structure. However, even after this modification, the ImportError
persisted when running script.py
in VSCode using Ctrl+F5, indicating an attempted relative import with no known parent package.