I have inherited quite a bit of python code and all over it is the following snippet which adds the file path of the parent directory to the system path.
from os.path import join, dirname
sys.path.insert(0, join(dirname(sys.argv[0]), "..\\"))
from utilities import find, execute
My understanding of this is that it adds a path to the search path. Which during the running of a program adds numerous pathsto the search path and presumably make it slower. As each file adds it's own parent directory.
I prefer the syntax
from scm_tools.general.utilities import find, execute
because this is easier to understand and far less code. This might have implications if I am moving the code around but it's all in a single package.
Am I right in assuming that inside a package that the latter syntax is the more pythonic way of doing things ?
or does it not really matter as under the hood python is doing some magic ?
from ..utilities import find,execute... but yeah generally speaking it is bad form to edit the path from inside your program (every rule has exceptions however)scm_tools?), then your preferred syntax is much better.scm_toolscould mask other top level modules. Suppose somebody createsscm_tools/xml.py- a perfectly reasonable thing to do. Then you spend a day wondering why your deployments are crashing onimport xml.etree.ElementTree.