Jupyter Notebook ImportError: cannot import name 'example_var'
Asked Answered
C

2

6

When I change/add a variable to my config.py file and then try to import it to my Jupyter Notebook I get:

ImportError: cannot import name 'example_var' from 'config'

config.py:

example_var = 'example'

jp_notebook.ipynb:

from config import example_var

print(example_var)

But after I restart the Jupyter Kernel it works fine until I modify the config.py file again. I read somewhere that it's because jupyter already cached that import. Is there any other way to delete that cache so I don't have to restart the kernel every time I make a change in the config.py file. Thanks for help in advance.

Corum answered 20/1, 2021 at 17:8 Comment(0)
S
6

You can use autoreload to reload modules every new cell execution.

%load_ext autoreload
%autoreload 2
from config import example_var

print(example_var)
Styrene answered 20/1, 2021 at 17:48 Comment(0)
C
0

Also works in Notebooks when you work in a .py file and you always need a reloaded version in the notebook.

enter image description here

Ctenidium answered 28/6, 2022 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.