1

I have already seen similar questions to the same topic, but couldn't find a solution for my particular problem.

I have a class definition in a python submodule that I import in my main python script. As the class uses a library that needs the "__name__ == __main__"-protective wrapper for parallel computation in Windows, I use "__name__ == __main__" in my main python script.

I want to avoid the protective wrapper in my main script, though. Is it possible to have the "__name__ == __main__"-protective wrapper in the python submodule where I define the class?

I work on a Windows machine and the python library that needs the protective wrapper is called TBATS (https://github.com/intive-DataScience/tbats).

mercator
  • 28,290
  • 8
  • 63
  • 72
Jan
  • 131
  • 1
  • 11
  • 3
    you only need that in a script that's designed to be either imported or run as a top-level script. If your main script shouldn't be imported you don't need the wrapper. – Barmar Jun 12 '20 at 16:37
  • 2
    Have you read https://stackoverflow.com/questions/419163/what-does-if-name-main-do? – mercator Jun 12 '20 at 16:38
  • Yes I had a look at that thread. Apparently TBATS is calling itself during model fit and therefore it needs the protective wrapper. – Jan Jun 12 '20 at 16:40
  • I wouldn't call it a "protective wrapper" at all. Seems a mischaracterization of the purpose. – Charles Duffy Jun 12 '20 at 17:58
  • If I run `python -m my.module.name ...args...`, then I'm intentionally running the code inside that block as a main function / command-line entry point. If I run `import my.module.name`, then I'm making its functions available for other code to use. These are distinctive and separately-useful purposes for a module to be put to; it makes sense to have separate codepaths between them. – Charles Duffy Jun 12 '20 at 17:59

0 Answers0