1

In Python (I use Spyder), is there any way to unlink all the libraries (packages) and clear all data? In R, we have rm(list=ls()). I wonder if there is an equivalent in Python.

  • What exactly are you trying to achieve? After `import module` you can do `del(module)` - is that what you're after? If you need it unloaded, you can try `del sys.modules['module']` - but I'd recommend against it unless you have very clear reasons to need this. – Grismar May 18 '20 at 01:17
  • Does this answer your question? [How do I unload (reload) a module?](https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-module) – Grismar May 18 '20 at 01:24
  • @Grismar I would like to add a line at the beginning of the program to unload all the packages and let the program forget all the variables. Let everything start afresh. – user2644687 May 18 '20 at 01:45
  • Whenever you run a script, it will have a 'fresh' environment already - you shouldn't unload everything, because many things are loaded because your script will need them. Unless you're developing an IDE or something of the sort, what you're trying to do is probably not a good idea and not really needed in a normal Python context. Mind you, Python is not R. Also, I think the best possible answers are already provided in the linked question. – Grismar May 18 '20 at 01:56

1 Answers1

0

try this: How to unimport a python module which is already imported?

>>> import requests
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'readline', 'requests', 'rlcompleter']
>>> del requests
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'readline', 'rlcompleter']
>>>
tritium_3
  • 648
  • 1
  • 8
  • 17