loadingbar
loadingbar

Reputation: 61

Functions imported from another file can't call the variables from main file

here's my main.py

text = "hello"

def print_text() :

    print(text)

print_text()

it gives the desired output hello , now i want to split my code into two files like this

main.py

import functions

text = "hello"

functions.print_text()

functions.py

def print_text() :

    print(text) 

It gives an error as - NameError: name 'text' is not defined

Please Note that if possible i would like to call the variable from main.py not defining it on functions.py .

Thanks in advance and sorry if i did something on explaining my issue

Upvotes: 0

Views: 68

Answers (1)

Anon
Anon

Reputation: 2666

functions.py

def print_text(text) :      <-- this is the change

    print(text) 

main.py

import functions

text = "hello"

functions.print_text(text)   <-- change here.

So now you have defined the variable in main.py and still able to call functions method.

Upvotes: 3

Related Questions