'cannot fix: attempted relative import beyond top-level package
I have read many of the related posts about this but cannot figure out how to fix the following.
Here is a link to a zip of this super simple example. https://1drv.ms/u/s!Auri_uzbtK_to4AGo1yammz7PRrv6w?e=QOtQeL
I am using VS Code. I have the following python program structure shown below.
I am trying to run and debug "run_tests.py". When I do, VS Code throws the error "attempted relative import beyond top-level package"
The line in question is in file helper.py "from ...inpatient.model import Model"
I am stuck on how to fix this. Any help would be greatly appreciated.
MyApp/
inpatient/
__init__.py
model.py
tests/
__init__.py
run_tests.py
utils/
__init__.py
helper.py
venv
run_tests.py
import unittest
from pathlib import Path
import os
import sys
from utills.helper import some_method
class BaseFludlTestCase(TestCase):
def setUp(self) -> None:
if __name__ == "__main__":
unittest.main()
helper.py
from ...inpatient.model import Model
def some_method():
model = Model()
Solution 1:[1]
Ultimately, my misunderstanding was that "import" in python were relatively to where the main program was executed.
with this new knowledge, I moved my running code to the root and now things operate as a expect.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DapperDanh |
