Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
If I have a srting inside my Python code that looks like this:
x = "print('Hello World')"
I want to execute it as if it is like a seperate .py file. Is there something like
execute(x)
exec()
eval
exec
exec(x)
See the documentation
But be careful of injection vulnerabilities: if the string comes from a user, they will have the power to control your computer - e.g. something like __import__('shutil').rmtree('/') could remove a whole directory.
__import__('shutil').rmtree('/')
Add a comment
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
exec()see docs.python.org/3/library/functions.html#exec. Also see this thread for a discussion on the topic, and a comparison of the functionsevalandexec.exec()should be avoided: stackoverflow.com/questions/1933451/…