How to modify variables in another python file?
<p><strong>windows 10 - python 3.5.2</strong></p>

<p>Hi, I have the two following python files, and I want to edit the second file's variables using the code in the first python file.</p>

<p><strong>firstfile.py</strong></p>

<pre><code>from X.secondfile import *

def edit():
    #editing second file's variables by user input
if Language == 'en-US':
    print('language is English-us')
elif Language == 'en-UK':
    print('language is English-uk')
</code></pre>

<p><strong>secondfile.py</strong></p>

<pre><code>Language = 'en-US'
</code></pre>

<p>i can add some variables to it by following code, but how can i edit one ?</p>

<pre><code>with open("secondfile.py","a") as f:
    f.write("Language = 'en-US'")
</code></pre>

<p>Any ideas how to do that?</p>

<p>This can be done to edit a variable in another file:</p>

<pre><code>import X.secondfile
X.secondfile.Language = 'en-UK'
</code></pre>

<p>However, I have two suggestions:</p>

<ol>
<li><p>Don't use <code>import *</code> - it pollutes the namespace with unexpected
names</p></li>
<li><p>Don't use such global variables, if they are not constants. Some code will read the value before it is changed and some afterwards. And none will expect it to change.</p></li>
</ol>

<p>So, rather than this, create a class in the other file.</p>

<pre><code>class LanguagePreferences(object):
    def __init__(self, langcode):
        self.langcode = langcode

language_preferences = LanguagePreferences('en-UK')
</code></pre>

python variables module