0

I'm getting an error while trying to access a variable from a function which is included using exec(open().read()).

But here I'm able to access the function printName(). Is there any other way without using subprocess?

I'm using python 3.7

main.py

age = 32
def printName(name):
    print(name)
exec(open("action.py").read())

action.py

printName("John")
print(age)

John

NameError: name age is not defined

1 Answer 1

1

I tried this code you are mentioned is working I think you are running action.py technically you need to run the main.py file that contained this code.

age = 32
def printName(name):
    print(name)
exec(open("action.py").read())

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.