0

There are a few question (and answers) about unimporting a module. All of them quite old, and one even indicate on about 2010 a Python reply not possible for the next 5 years. Well now we are a few version and years away from that statement.
Perhaps, still not possible, but any work around?
I have a web application that run arbitrary external code, importing, testing and calling the script by functions name.

__import__(ExternalCodeScriptName.replace('.py',''))

Assume we import a few hundred, how can I 'unimport' the unused? Perhaps I can take a current state picture and the restore the main app to that state? To restart the application is not a valid option.

fcm
  • 1,247
  • 15
  • 28
  • Do you really mean uninstalling (`pip uninstall module_name`)? Or do you mean unimporting (the opposite of `from x import y`)? – thclark Jun 27 '19 at 11:44
  • Possible duplicate of https://stackoverflow.com/questions/32234156/how-to-unimport-a-python-module-which-is-already-imported – thclark Jun 27 '19 at 11:59
  • Use `del`. Say you `import importlib`, use `del importlib`. That is, however, if you don't keep it elsewhere such as by doing `copy_importlib = importlib`. This uses reference counting and might not work on other Python implementations. – GeeTransit Jun 27 '19 at 12:30
  • it's indeed 'unimporting' title and content fixed. yes, I know is duplicate, as indicated on my text, but does Python advance making old answer obsolete? Previous answers, mostly unanswered, refer to Python 2 and not 3? 32234156 does not answer the question loosing access wont free the memory. – fcm Jun 27 '19 at 12:36

0 Answers0