0

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)
4

1 Answer 1

5
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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.