Import different files based on where script is being called from
<p>I have a file <code>global_params.py</code> which defines some global parameters that are used by a script <code>client_script.py</code>.</p>

<p>Now <code>client_script.py</code> can be called from the terminal, or it can be called from another python script <code>caller.py</code>. When it's called from the terminal I want the global parameters to be loaded from <code>global_params.py</code> I can do that with:</p>

<pre><code>if __name__ == '__main__':
    from global_params import *
</code></pre>

<p>When <code>client_script.py</code> is called by <code>caller.py</code> how do I make it load global parameters from another file, say <code>global_params_2.py</code></p>

<p><code>__name__</code> has the name of the caller, it is <code>__main__</code>  if you call <code>client_script.py</code> from the terminal, and the name of the calling script if it is called from another script. If <code>client_script.py</code> is called from within <code>caller.py</code>  then <code>__name__</code> is <code>caller</code></p>

<p>So in your case this should do what you want:</p>

<pre><code>if __name__ == 'caller':
    from global_params_2 import *
</code></pre>

python python-2.7 import