Include one python file B inside file A where B uses variables defined in A
<p>I have a Django project where I have multiple settings file which has lot of redundant data. The structure is as follows</p>

<p><strong>development.py</strong></p>

<pre><code>app_one = 'http://localhost:8000'
app_two = 'http://localhost:9999'

abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)
</code></pre>

<p><strong>staging.py</strong></p>

<pre><code>app_one = 'http://staging.xyz.abc.com'
app_two = 'http://staging.pqr.abc.com'

abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)
</code></pre>

<p><strong>production.py</strong></p>

<pre><code>app_one = 'http://production.xyz.abc.com'
app_two = 'http://production.pqr.abc.com'

abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)
</code></pre>

<p>In all the files <code>abc_url</code> and <code>xyz_url</code> are basically same url. The only thing that changes is the domain.</p>

<p>What I am looking to is,</p>

<ul>
<li>Put all urls in separate file called app_one_urls.py and app_two_urls.py</li>
<li>Find a way to include this app_one_urls.py and app_two_urls.py in my development, staging and productions file</li>
</ul>

<p>The final outcome can be as follows:</p>

<p><strong>app_one_urls.py</strong></p>

<pre><code>abc_url = '{0}/some-url/'.format(app_one)
</code></pre>

<p><strong>app_two_urls.py</strong></p>

<pre><code>xyz_url = '{0}/some-url/'.format(app_two)
</code></pre>

<p>are two separate files</p>

<p>in development.py I intend to do following</p>

<pre><code>app_one = 'http://localhost:8000'
app_two = 'http://localhost:9999'
somehow get urls from app_one_urls and app_two_urls
</code></pre>

<p>Is it possible and feasible? If yes, I need help in understanding how.</p>

<p>There is no need to maintain separate files, you can define the configuration in dictionary, with <code>key</code> based on the environment type.</p>

<p>Here I am demonstrating based on <code>hostname</code>, as hostnames of my server differs like: <code>my-host-prod</code>, <code>my-host-staging</code>, <code>my-host-dev</code>. <em>You may use the condition which uniquely defines your server.</em></p>

<pre><code>import socket

def get_url_conf(my_host=socket.gethostname()):
    def get_conf_setting(env_type):
        return {'prod':    {'app1': 'app1_prod_url',
                            'app2': 'app2_prod_url'},
                'staging': {'app1': 'app1_staging_url',
                            'app2': 'app2_staging_url'},
                'dev':     {'app1': 'app1_dev_url',
                            'app2': 'app2_dev_url'},
                'local':   {'app1': 'app1_local_url',
                            'app2': 'app2_local_url'}
                }[env_type]
    if my_host.endswith('-prod'):
        server_key = 'prod'
    elif my_host.endswith('-staging'):
        server_key = 'prod'
    elif my_host.endswith('-dev'):
        server_key = 'dev'
    else:  # In case someone is running on local system
        server_key = 'local'
    return get_conf_setting(server_key)
</code></pre>

<p>Now in your settings file, you may call these as:</p>

<pre><code>abc_url = '{0}/some-url/'.format(get_url_conf()[`app1`])
xyz_url = '{0}/some-url/'.format(get_url_conf()[`app2`]) 
</code></pre>

python django