I've a whole bunch of scripts organized like this:
root
group1
script1.py
script2.py
group2
script1.py
script2.py
group3
script1.py
script2.py
utils
utils1.py
utils2.py
All the scripts*.py use functions inside the utils folder. At the moment, I append the utils path to the scripts in order to import utils.
However, this seems to be bad practice (or "not Pythonic"). In addition, the groups in actuality is not as flat as this and there are more util folders than listed above. Hence, the append path solution is getting messier and messier.
How can I organize this differently?
Make all your directories importable first i.e. use __init__.py
.
Then have a top level script that accepts arguments and invokes scripts based on that.
For long term what Keith has mentioned about distutils holds true. Otherwise here is a simpler (sure not the best) solution.
Organization
runscript.py
group1
__init__.py
script1.py
utils
__init__.py
utils1.py
Invocation
python runscript -g grp1 -s script1
runscript.py
import utils
def main():
script_to_exec = process_args()
import script_to_exec as script # __import__
script.main()
main()
Maybe your script can have main function which is then invoked by runscript. I suggest that you have a script at the top level which imports the script.