Ned Hulton Ned Hulton - 4 months ago 7
Python Question

Python variable scope and execution

I am executing a python script:

exec(open("").read(), globals())

Is it possible to make all variables set within "" local to that particular file?


You could execute the file's contents in a new dict instead of this code's own globals:

with open("") as f:
    exec(, {})

This is a weird way to structure a Python program, though. Usually, you'd organize your code into modules and functions instead of exec-ing files.