Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to Put Python Utils Folder?

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?

like image 857
Gilbert Avatar asked Mar 08 '11 07:03

Gilbert


People also ask

Where do I put utils PY?

Move into your project page, and click the New buttom and select the Blank File . Then name the file utils.py and press Enter key. Select the file and click Edit File . Copy and paste the content of utils.py from the tutorial Github repo, and click Save File .

What is Utils folder in Python?

3.1. 11 The util Directory This directory contains several utility programs and libraries. The programs used to configure and build the code, such as autoconf , lndir , kbuild , reconf , and makedepend , are in this directory.

What is the Utils folder for?

Utils Folder Here goes everything that controls your app like constants, assets, enums, lang folders, routes, styles, etc.

How do I import utils in Jupyter notebook?

If you want to use the utils package, install it with pip install utils . Otherwise, use import python_utils if you want to use that package.


1 Answers

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.

like image 162
Shekhar Avatar answered Sep 18 '22 02:09

Shekhar