Reimporting a single function in python

Using python in interactive mode, one imports the module, and then if the module is changed (fixing a bug or something else), you can simply use the reload () command.

But what if I did not import the entire module and use the import statement from m import f, g. Is there a way to reimport only g?

(I tried to remove the function from the parameter table using 'del g' and delete the .pyc file from the directory. That did not help. When I re-imported the function 'from M import g', the old g was loaded).

+3
source share
3 answers

from foo import bar, . bar . .

reload (, C). .

+9

, , - :

def my_reload(mod, name):
    reload(mod)
    globals()[name] = getattr(mod, name)

myreload(somemodule, "some_function")

. , , , , . , reload . , , , __name__. , __name__, - , .

, , , reload, . , .

+1

( ):

import numpy as NP
import numpy.linalg as LA
  ...
reload(LA)
0

Source: https://habr.com/ru/post/1779143/


All Articles