Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload a function in Python?

I want to insert two different paths with the same name of python file. Is it possible to reload goal_func without reestarting the kernel?

step 1:

path = 'a*/goal.py'
ruta_carpeta = path[:-8]
sys.path.insert(1, ruta_carpeta)
from goal import goal_func

step 2:

path = 'b*/goal.py'
ruta_carpeta = path[:-8]
sys.path.insert(1, ruta_carpeta)
from goal import goal_func

goal_func has not been updated.

like image 423
joz Avatar asked Jul 10 '26 22:07

joz


1 Answers

Use importlib.reload on the module, then reimport the function:

import importlib
import goal
importlib.reload(goal) # reload the module
# now import the function
from goal import goal_func
like image 153
Aplet123 Avatar answered Jul 14 '26 01:07

Aplet123