Ned Hulton Ned Hulton - 1 year ago 107
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download