Where to put the Python Utils folder?

I have 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 * .py scripts use functions inside the utils folder. At the moment, I am adding the utils path to the scripts to import utils.

However, this seems like bad practice (or "not Pythonic"). In addition, the groups are actually not as flat as this, and there are more folders than indicated above. Therefore, the decision to add paths becomes more messy and messy.

How can I organize it differently?

+4
source share
4 answers

Import all your directories first, and then use __init__.py . Then use the top level script, which takes arguments and calls scripts based on them.

In the long run, what Kate mentioned about distutils is true. Otherwise, this is a simpler (certainly not the best) solution.

Organization

 runscript.py group1 __init__.py script1.py utils __init__.py utils1.py 

Vocation

 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() 

Perhaps your script may have a main function, which is then called by the script. I suggest you have a top-level script that imports the script.

+3
source

You may also be interested in: Python 3: Good places to install your own and third-party packages?

Yes, I see how packages are help, but there are too many overheads to install if you are still working with utils and changing them when working with group1, group2, etc.

We are looking for a good answer to this question.

+1
source

We cannot know what your use case is from a far-fetched example. I would recommend making a package containing your utils module and other scripts, and then use relative import from scripts, for example from ..utils import utils1 . Of course, you would not call the top package "root", but you would choose a name similar to the name of your project.

If you really have a lot of such scripts, it might make sense to have one script run, which then imports the modules according to the cmdline parameters. Then something like runner.py <somecommand> will import and execute <top package>.commands.somecommand.runFunction (for example, Django makes this project extensible).

+1
source

You need to use packages. Python recognizes a directory as a package when it has a file named __init__.py . It is better to still use distutils to create the source package, which will also install it for you. Changing sys.path will almost never be done, and installing packages will put it in the standard location (site-packages), so you don't need to change it.

0
source

Source: https://habr.com/ru/post/1342766/


All Articles