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?
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.
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.
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.
autoreload reloads modules automatically before entering the execution of code typed at the IPython prompt.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With