python - Is adding folders to system path for reaching other modules is a dirty hack? -
i have commands.py module in directory contain subdirectory script. don't want copy in every folder, contain scripts. bad practice?
import sys sys.path.append("..") # add previous folder run script sys.path.append(".") # add current folder if run script in folder, contain commands.py commands import *
sorry bad english.
current folder first entry in sys.path
(from python's path-making perspective '' == '.'
) adding pointless in second case.
the first case more problematic - first of all, current path should first entry if insist on adding folders sys.path
@ least insert them @ index 1+
, or better, append path end of sys.path
ensure built-in , current-folder visible modules accessible intended locations.
finally, due fact different parts using sys.path
search path in different ways, setting relative paths not idea. if want hardcode parent path, use os.path.realpath("..")
.
this under assumption there no other way - i'd still urge rethink design , avoid messing search path entirely.
Comments
Post a Comment