3

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.

3
  • have you tried importlib.reload ? Commented Nov 16, 2020 at 15:13
  • Yes, I have tried it, but it tells me that it is not a module. Commented Nov 16, 2020 at 15:18
  • Reloading modules is not recommended except for playing in the Python console. What is your more general objective here? Commented Nov 19, 2020 at 19:59

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

import imp
imp.reload(function/module)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.