pythonsyntax-error

How do I get a variable from another file, but use a functions string to define the variable


I have set a variable x="hii!!" in another file called ImporterFile.py. I thought pre-defining the variable x and then re-importing it would work, but it didn't.

from ImporterFile import x

def grab(string):
    from ImporterFile import (string)

grab(x)

The original problem was that when running the file it would say that "string" wasn't a variable that existed in that file.


Solution

  • If you want to import x and use it in another file, you just need to import it once. There's no need for another import or a "grab" function.

    main.py:

    from ImporterFile import x
    
    print(x)
    

    ImporterFile.py:

    x = "hii!!"