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>Yes, this is feasible.
You will need to arrange your settings as a module:</p>

<pre><code>settings/
        /__init__.py
        /development.py
        /staging.py
        /production.py
        /base.py
</code></pre>

<p>Contents of base.py :</p>

<pre><code>SOME_ENVIRONMENT_INDEPENDENT_SETTING = 1
</code></pre>

<p>Contents of development.py:</p>

<pre><code>from base import *
SOME_DEVELOPMENT_SETTING = 1
app_one = 'http://localhost:8000'
app_two = 'http://localhost:9999'
</code></pre>

<p>Content of production.py:</p>

<pre><code>    from base import *
    SOME_PRODUCTION_SETTING = 1
    app_one = 'http://production.xyz.abc.com'
    app_two = 'http://production.pqr.abc.com'
</code></pre>

<p>Content of <code>__init__.py</code>:</p>

<pre><code>import os
#If you want to pull environment from an environment variable

#ENVIRONMENT = os.environ.get('CURR_ENV','PROD') 

ENVIRONMENT = "DEVELOPMENT"

if ENVIRONMENT == "PRODUCTION" :
    try:
        from production import *
    except:
        pass

elif ENVIRONMENT == "DEVELOPMENT" :
    try:
        from development import *
    except:
        pass

elif ENVIRONMENT == "STAGING":
    try:
        from staging import *
    except:
        pass

elif ENVIRONMENT.lower() == "DEVEL_LOCAL".lower():
    try:
        from devel_local import *
    except:
        pass

elif ENVIRONMENT.lower() == "PROD_PP".lower():
    try:
        from prod_pp import *
    except:
        pass


#common variables which change based on environment
abc_url = '{0}/some-url/'.format(app_one)
xyz_url = '{0}/some-url/'.format(app_two)
</code></pre>

python django