joz
joz

Reputation: 31

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.

Upvotes: 3

Views: 1575

Answers (2)

Aplet123
Aplet123

Reputation: 35482

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

Upvotes: 4

Ruhan Sharief
Ruhan Sharief

Reputation: 111

There is a reload function in the 'imp' module. Using this we can reload the imported ones.

import imp
imp.reload(function/module)

Upvotes: 0

Related Questions