10

I have the following folder structure

project/
  src/
    __init__.py
    mymodule.py
  mynotebook.ipynb

within mynotebook I can import mymodule using standar formula from src.mymodule import *. The problem pops up when modifying mymodule and trying to reimport it without stopping the kernel. I am following this discussion but it is not working. (python ver: 3.3.5)

from imp import reload 
reload(src.mymodule) # also reload(mymodule)

the code above fails with message name 'src' is not defined (also name 'mymodule' is not defined). I can't use ipython's autoreload because I have no permissions to install it.

Thanks!

2 Answers 2

13

You need to import src too and then reload(src.mymodule).

from src import mymodule
import src
# Change in mymodule
reload(src.mymodule)
Sign up to request clarification or add additional context in comments.

2 Comments

Quick note for python3: use importlib.reload
@JamesOwers BTW, do those magic commands work in Jupyter notebook? Such as %autoload 2?
3

This embellishes James Owers's comment from the Python3 documentation @ https://docs.python.org/3/library/importlib.html

This is my module import, when I edit the model I need to reload it.

import src.project.model as Mdl

This gives us the reload function

from importlib import reload

.. and when you want to reload your module in a cell

reload(Mdl)

2 Comments

What if I just load bunch of function by from MyFile import *? How can I reload all functions?
What have you tried? If you've an issue that isn't covered you could make a question.

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.