Search code examples
pythonvariables

How do I pass a variable from an external script to the main?


I'm trying to make a python library and I need to pass the variable x to the main document.

external.py:

def get_x():
     x = '123'

main.py:

import external

external.get_x()
print(x)

This ofcourse wouldn't work as x is not defined in main.py, right?


Solution

  • external.py:

    def get_x():
         x = '123'
         return x
    

    main.py:

    import external
    
    x=external.get_x()
    print(x)