Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython %run vs. import for loading settings

I'm unable to find anything meaningful by searching these keywords, so I'm asking here.

What is the main difference between IPython's (when running in a Jupyter notebook) %run and Python's import? If I'd like to import some settings (say, for Matplotlib), for multiple notebooks, which one shall I use?

like image 881
Atcold Avatar asked Feb 24 '17 14:02

Atcold


People also ask

How do I load files into IPython?

A text file can be loaded in a notebook cell with the magic command %load . the content of filename.py will be loaded in the next cell. You can edit and execute it as usual. To save the cell content back into a file add the cell-magic %%writefile filename.py at the beginning of the cell and run it.

What is import IPython?

IPython imports are just regular Python imports, but running it inside a package is problematic, because Python doesn't treat the working directory as a package.

What is the difference between IPython and Jupyter?

IPython continued to exist as a Python shell and kernel for Jupyter, but the notebook interface and other language-agnostic parts of IPython were moved under the Jupyter name. Jupyter is language agnostic and its name is a reference to core programming languages supported by Jupyter, which are Julia, Python, and R.

What is Autoreload in Jupyter Notebook?

autoreload reloads modules automatically before entering the execution of code typed at the IPython prompt.


1 Answers

It seems I'm a bit late to the party... For me the most meaningful difference between using import and %run is felt when developing packages, form the docs: "Since the file is re-read from disk each time [ when using %run ] , changes you make to it are reflected immediately (unlike imported modules, which have to be specifically reloaded)"

In practice this means that I need to restart the notebook kernel when I make changes to an imported package which I would like to test, where with %run everything is reflected immediately.

like image 151
EduardKieser Avatar answered Oct 22 '22 04:10

EduardKieser