example directory structure and code:
JavaScript
x
10
10
1
root/
2
├─ main.py
3
├─ library/
4
│ ├─ __init__.py
5
│ ├─ library_code.py
6
│ ├─ utilities/
7
│ │ ├─ __init__.py
8
│ │ ├─ utils_one.py
9
│ │ ├─ utils_two.py
10
JavaScript
1
5
1
# main.py
2
3
import library as lb
4
# lb.library_code is how you would access stuff
5
JavaScript
1
4
1
# library/__init__.py
2
import [library.]library_code
3
import [library.]utilities
4
JavaScript
1
4
1
# library.utilities.__init__.py
2
import [library.utilities.]utils_one
3
import [library.utilities.]utils_two
4
how would you go about removing the text [inside these square brackets] on the import statements?
of course, doing this kind of structure will cause this to not be possible:
JavaScript
1
4
1
# library/utilities/utils_one.py
2
3
import library.library_code
4
Advertisement
Answer
The simplest is to just use a relative import. For example in utils_one.py you can do
JavaScript
1
3
1
from . import utils_two
2
from .utils_two import function_one
3
You can also use ..
to go up a hierarchy just like with a file system