How to call a variable inside main function from another program in python?
<p>I have two python files first.py and second.py</p>

<p>first.py looks like</p>

<pre><code>def main():
  #some computation
  first_variable=computation_result
</code></pre>

<p>second.py looks like</p>

<pre><code>import first
def main():
  b=getattr(first, first_variable)
  #computation
</code></pre>

<p>but I am getting No Attribute error. Is there any way to access a variable inside main() method in first.py through second.py?</p>

<p>You should use function calls and return values instead of this. </p>

<p>Return the computation_result from the function in the first file, and then store the result in the b variable in the second file.</p>

<p>first.py</p>

<pre><code>def main():
    # computation
    return computation_result
</code></pre>

<p>second.py</p>

<pre><code>import first
def main():
    b = first.main()
</code></pre>

<p>Other option is to use a global variable in the first file where you will store the value and later reference it.</p>

python