开发者

Where to Put Python Utils Folder?

I've a whole bunch of scrip开发者_如何学编程ts 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.


You need to use packages. Python recognizes a directory as a package when it has a file named __init__.py in it. Better still use distutils to make a source package which will also install it for you. Modifying sys.path should almost never be done, and installing packages will place it in a standard place (site-packages) so you don't have to modify it.


You might also be interested in: Python 3: Good location(s) to install own and 3rd party packages?

Yes, I can see how packages are a help, but making an install is a lot of overhead if you are still working on the utils and changing them as you work on group1, group2 etc.

Still looking for good answer to this.


We can't really know what your use case is from the contrived example. I would recommend to make a package containing your utils module and the other scripts, and then using relative imports from the scripts, such as from ..utils import utils1. Of course you wouldn't call the top package "root" but choose a name like your project name.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜