简体   繁体   中英

Jupyter Notebook ImportError: cannot import name 'example_var'

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.

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

%load_ext autoreload
%autoreload 2
from config import example_var

print(example_var)

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM