Relative imports are for use within packages. You could change what you have into one by adding some empty __init__.py
files into your directory structure like this:
d1
- test.py # added to run script.py below
- app
- __init__.py # an empty script
— script.py
- lib
- __init__.py # an empty script
— lib1
- __init__.py # an empty script
— file.py
There's also a new file I've called test.py
because you genrally can't run modules within a package as the main script (which is why I added test.py
). All it contains is:
from app import script
I also changed the import in script.py
to this:
from lib.lib1 import file
To show that the above import
works, I put a print('in file.py')
in file.py
.
Now running test.py
, which runs the script
module by importing it, produces the following output:
in file.py
Now, if you want to import a particular function from file.py
, you could do something like this in script.py
:
from lib.lib1.file import my_func