Here's my project structure:
py_workshop/
├── app/
│ ├── __init__.py
│ ├── script.py
│ └── time_module.py
└── tests/
└── test_time_module.py
time_module.py's content:
import datetime
def current_time():
return datetime.datetime.now().strftime("%H:%M:%S")
script.py's content:
from time_module import current_time
print(current_time())
test_time_module.py's content:
from app.time_module import current_time
print(current_time())
When I import current_time() from "app.time_module" into "test_time_module.py" I get this error:
Traceback (most recent call last): File "H:\dev\py-workshop\tests\test_time_module.py", line 1, in from ..app.time_module import * ImportError: attempted relative import with no known parent package
I've googled for a possible solution and found out that I should add init.py to app/ and time_module/ to fix the error but it resulted in the same error.
I this thread I found an answer that says I for a seperate unit as in my case I should change PYTHONPATH with PYTHONPATH=$PYTHONPATH${PYTHONPATH:+:}$PWD/lib/
so I ran:
PYTHONPATH=$PYTHONPATH${PYTHONPATH:+:}$PWD/app/
still no success!
I expect that when I run $ python tests/test_time_module.py I get the current time in this format: H:M:S
I'm very confused at this moment.
Edit 1:
Current solution:
import sys
sys.path.append('../')
if __name__ == '__main__':
from app.time_module import *
print(current_time())```