0

I have this module installed by me in my virtual env using pip install: concurrent_log_handler (best thing since sliced bread, by the way).

I want to create a test which will run the following app code:

try:
    from concurrent_log_handler import ConcurrentRotatingFileHandler 
except BaseException as e:
    print(f'e was |{e}|', file=sys.stderr)

... and want the framework to find that this module concurrent_log_handler is "not available", and thus print out for the user a suitable message like "you have to run 'pip install concurrent_log_handler'".

The thing is, this is not the same as being present in sys.modules: it is only after the above code that you find that a key "concurrent_log_handler" is now present in sys.modules.

Is there some sort of dict somewhere of "modules which have been installed but not yet imported"? If so, I surmise that deleting the key from that dict might do the trick...

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • Per [this question](https://stackoverflow.com/questions/28346798/python-testing-simulate-importerror) just inserting it into `sys.modules` with `None` as the value will cause an import error for you – 2e0byo Oct 23 '21 at 16:07
  • Just tested it and it works, in which case this is a duplicate. that does look like a useful module, btw; I've got something similar (but not concurrent) I wrote myself for small systems, but I'll definitely keep it in hand next time I need logging on a big system – 2e0byo Oct 23 '21 at 16:10
  • Yes, will delete assuming no-one comes up with something else. – mike rodent Oct 23 '21 at 16:11

0 Answers0