I need some help. I have been stuck with this for a while. I cannot figure out how and what to do.
I have a directory "folder" with the below structure.
.
├── folder1
│ ├── folder3
│ │ ├── a.py
│ │ └── __init__.py
│ └── __init__.py
├── folder2
│ ├── b.py
│ └── __init__.py
└── __init__.py
What I want to basically do is import a function in the "folder2/b.py" python file into "folder1/folder3/a.py". And I want to run the 'a.py' file from "folder1/folder3/" directory.
Let me illustrate this with an example. Suppose I have a function 'hello' in 'b.py'.
def hello():
print("Hello world!")
And I have another file 'a.py' like this.
from b import hello
def hello1():
print("Hello World!!")
I want to navigate to the 'folder3' directory.
cd folder1/folder3
And run the python file 'a.py' as follows.
python a.py
Doing so is giving me the below error.
ValueError: attempted relative import beyond the top-level package
I googled for a while and tried solving this with these resources (Import a module from a relative path, relative path not working even with init.py, How to fix “Attempted relative import in non-package” even with init.py).
The only method that worked so far is by using "sys.append" in 'a.py' like below. However, I have read that this usage is very unprofessional and highly unrecommended.
import sys
sys.path.append("D:/folder")
from folder2 import b
If you have dealt with such a setup/issue before, please help me out. Thanks!