0

I wrote this code:

import sys
import time


def change(str):
    with open(file, 'w') as func:
        if 'function' in sys.modules:
            sys.modules.pop('function')
            time.sleep(1)
        func.write(str)

import function
function.add(1, 2)

It doesn't work well (it might use older version of the module\function) without time-break between

  • sys.modules.pop('function')

and

  • import function

I am looking for a more time efficient solution.

  • `importlib.reload` does it fit? – iBug Jan 21 '22 at 02:46
  • Does this question answer your question https://stackoverflow.com/questions/16373510/improving-speed-of-python-module-import –  Jan 21 '22 at 02:47
  • i didn't manage to make it work using: importlib.reload @iBug – Urigal Urigal Jan 21 '22 at 03:05
  • @JesvinVijeshS I don't think the problem is the time required to import the module because the module I imported is very small and the additional time the program required for running is one sec. if I didn't make a break of one sec the program would run on the last module I imported – Urigal Urigal Jan 21 '22 at 03:09

0 Answers0