Reputation: 1207
I have two python files namely abc.py
and xyz.py
abc.py
contains two function
viz func_1()
and func_2()
My directory structure is as follows
db_ops /
__init__.py
abc.py
xyz.py
Now in xyz.py
I have the following import method
from db_ops.abc import func_1
When I run xyz.py
I am getting error as
ModuleNotFoundError: No module named 'db_ops'
Am I missing out anything?
P.S. I am using Windows 10
Upvotes: 0
Views: 130
Reputation: 1207
Ok so I found the answer for the problem I was facing.
We need to add the dir path
where abc.py
and xyz.py
are located.
Assuming we have the broad dir structure as follows
etl_works/
db_ops
__init__.py/
__init__.py
abc.py
xyz.py
And etl_works
directory is as follows
'D:/my_py/etl_works'
Then we need to add the above to the sys.path
i.e.
import sys
sys.path.append('D:/my_py/etl_works')
And then we can use import db_ops
or from db_ops import abc
Caveat:
The above method is not a permanent solution. Every time we close the program or kernel, the path.append
method gets reversed. To circumvent this, we simply add the above dir path to PYTHONPATH (on in Windows 10 system, we add the path to Environment and system properties)
Hope this helps to any newbies like me!!
Upvotes: 0
Reputation: 1024
You don't need a package for this, since all your code is in the same directory.
Simply use from abc import func_1
since xyz and abc are in the same directory
Upvotes: 1