There are several ways to achieve the goal, each of which has its advantages and disadvantages.
(some of them overlap with other answers. I do not want plagiarism, only to give an exhaustive answer).
Approach 1: the function should do it
One way to ensure that a function is run only once is to make the function itself workable by making it “remember” that it has already been called. This is more or less what is described by @eestrada and @qarma.
Regarding the implementation of this, I agree with @qarma that using memoization is the easiest and most ideotic way. There are some simple reminder decorators for python on the web. functools.lru_cache is part of the standard library. You can just use it like:
@functools.lru_cache def set_up():
The disadvantage here is that the responsibility for maintaining state may not be set_up , but just a function. It can be argued that it should be executed twice if it is called twice, and it is only responsible for calling it when it needs it (what if you really want to run it twice)? The general argument is that a function (in order to be useful and reusable) should not make assumptions about the context in which it is called.
Is this argument correct in your case? You decide.
Another disadvantage is that it can be caused by abuse of the memoization tool. Memoization is a tool closely related to functional programming and should be applied to pure functions. Remembering funciton means "no need to run it again, because we already know the result "and not" there is no need to run it again, because there is some kind of side effect we want to avoid. "
Approach 2: the one you find ugly ( if __name__=='__main__' )
The most common pythonic path that you already mention in your question uses the infamous if __name__=='__main__' .
This ensures that the function is called only once, because it is called only from a module named __main__ , and the interpreter ensures that there is only one such module in your process.
It works. There are no complications and reservations. This is a way to run the main code (including the installation code) is executed in python. It is considered pythonic simply because it is so damn common in python (since there are no better ways).
The only drawback is that it is possibly ugly (asterically-wise, not code-quality-wise). I admit that I, too, flinched the first few times when I saw it or wrote, but it grows on you.
Approach 3: using the python module import mechanism
Python already has a caching mechanism to prevent double import of modules. You can use this mechanism by running the installation code in a new module, and then import it. This is similar to @ rll's answer. It's simple:
# logging_setup.py from framework import logutils logutils.set_up()
Now every caller can run this by importing a new module:
# foo/daily_report.py import logging_setup
Since the module is imported only once, set_up is called only once.
The disadvantage here is that it violates the principle of “explicit is better than implicit”. That is, if you want to call a function, call it. Bad practice of running code with side effects during module import.
Approach 4: Monkey Patch
This is by far the worst of the approaches in this answer. Do not use it. But this is still a way to get the job done.
The idea is that if you do not want the function to be called after the first call, monkey-patch (read: vandalize it) after the first call.
from framework import logutils logutils.set_up_only_once()
Where set_up_only_once can be implemented as:
def set_up_only_once():
Disadvantages: your colleagues will hate you.
TL; DR:
The easiest way is to memoize with functools.lru_cache , but it may not be the best solution with code quality. It is up to you if this solution is good enough in your case.
The safest and most pythonic path, although not eye- if __name__=='__main__': ... , uses if __name__=='__main__': ...