Consider this simple folder structure:
root
Package1
x.py
y.py
Package2
z.py
Examples
main.py
Now our requirements are:
- x.py needs to import y.py
- z.py needs to import y.py
- main.py needs to import y.py and z.py
Below is what works:
x.py
import y
def x():
y()
y.py
def y():
pass
z.py
import package1.y as y
def z():
y.y()
main.py
import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
import package1.y as y
import package2.z as z
y.y()
z.z()
Questions:
- Is this the best and recommended way to setup imports in Python 3?
- I really don't like changing
sys.path
inmain
because it strongly binds assumptions about package locations inside code file. Is there any way around that? - I also really don't like superfluous
as y
part inimport package1.y as y
. Is there any way around that?