2

Context: I am making a command line tool suite that will be launched much like git and all of it's tools. Commands will be run like meke toolname --args

Based on "Why Lazy Import is not default in Python?" it sounds my case is one of the few useful places for lazy importing. I would like to parse the command line to get which tool the user would like to run, then import that tool and pass along the remaining arguments to it's main function.

Question: Is this a good idea? Are there reasons I should import everything up front instead? Or perhaps there is a better way to structure the whole suite entry point?

I am pretty sure lazy imports will be better here. I figure a sanity check with you smart folks was a good way to prevent unforeseen complications or bad design practices. Thanks.

Community
  • 1
  • 1
Cory-G
  • 1,025
  • 14
  • 26
  • 1
    *"programmes may only run for a short time, and not use all of their features"* sounds pretty much like your use case, but how long is it really going to take to import everything anyway? – jonrsharpe Aug 07 '15 at 21:47
  • not long, but multiply that by thousands of times and it could be significant. Lazy importing is easy, so I figure the time to get it work is a fraction of the time it could save in the end. – Cory-G Aug 10 '15 at 16:45
  • For a bit more context: I am wanting to make an open-source build tool. I want it to behave like git. One would essentially call `meke init` to init a project directory then call `meke add` to add files. Once you build up your dependency tree and stuff, you can specify various build option and targets. So each command is kinda a program of it's own accord. The `meke` python script (meke because it is make, but more 'e'sy. nyuk nyuk) would pretty much just import and run the other sub-programs. – Cory-G Aug 10 '15 at 16:55

1 Answers1

1

Assuming you have a fair number of non-trivial tools in your suite, then yes it probably makes sense to do lazy imports. The real benefit you'd be targeting would likely have more to do with the recursive imports of libraries that those tools depend on rather than the tools themselves, but that still makes it worthwhile.

That said, why don't you test it out yourself and see? You can use timeit to watch execution time with and without lazy imports, and you can look at this SO question for discussion about watching memory usage -- Heapy would seem to be the default answer there. Look at the differences and see what makes sense to you.

Community
  • 1
  • 1
  • Heapy looks like a great tool to use. It sounds like there are no drawbacks to lazy importing this way, other than a bit more complexity. I can limit the complexity, so I will forge ahead and test it later. – Cory-G Aug 10 '15 at 16:49