How to call a variable inside main function from another program in python?
Question: I have two python files first.py and second.py

first.py looks like

    
    
    def main():
      #some computation
      first_variable=computation_result
    

second.py looks like

    
    
    import first
    def main():
      b=getattr(first, first_variable)
      #computation
    

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


Answer: You should use function calls and return values instead of this.

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

first.py

    
    
    def main():
        # computation
        return computation_result
    

second.py

    
    
    import first
    def main():
        b = first.main()
    

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

