I am facing an issue that is related to import path. I have a library file (func_a.py) as follows. This file is called from different directories. In such a case, how do I specify import path in client.py?
.
├── main.py
└── package_a
├── __init__.py
├── client.py
└── func_a.py
The codes are as follows:
$ cat package_a/func_a.py
def something():
print('something')
$ cat package_a/client.py
import func_a
func_a.something()
$ cat main.py import package_a.func_a as func_a
import package_a.client as client
func_a.something()
This is the error. When I call client.py, the file misses func_a.py since the current directory is the root, not package_a/.
$ python main.py Traceback (most recent call last):
File "main.py", line 2, in <module>
import package_a.client as client
File "/home/jef/work/test/package_a/client.py", line 1, in <module>
import func_a
ModuleNotFoundError: No module named 'func_a'
My python is 3.6. Thank you for your help.
Update
Although calling main.py is OK, calling client.py failed. I make both work.
$ cat client.py
from package_a import func_a
func_a.something()
$ python client.py
Traceback (most recent call last):
File "client.py", line 2, in <module>
from package_a import func_a
ModuleNotFoundError: No module named 'package_a'