7

After I import a module in python, how could I undo the effect in the same python work space?

For example, after I typed "import random" in a python IDLE, I want to remove all the imported functions in the module "random" in the same workspace, how could I do it?

Cindy
  • 163
  • 2
  • 9

1 Answers1

3

You could try to wrap the import and the code using that import in a specific scope. That way once the scope exits, the imported library won't be in reach.

def do_something_with_random():
    import random
    print("do something interesting with random", random.choice([0, 1]))

print("perform task that don't need random")
do_something_with_random()
print("function call uses random, but doesn't add it to your globals")
print("continue performing tasks that don't need random")
aripy887
  • 107
  • 1
  • 4