I am using a jupyter lab notebook and trying to modify code, reload it within the jupyter notebook and use the modified code without reloading the kernel. I am using python 3.5.5 and am running code like this:
(in file test.py)
def myTest():
print('hello')
(in jupyter)
from test import myTest
import importlib
importlib.reload(test)
myTest()
When I run the code in my jupyter lab notebook I get a NameError that name 'test' is not defined. From searching on stackoverflow the only references I find to this error is problems using older version of python. But the way I am using importlib.reload() seems to be correct.
Have you tried the built-in magic command autoreload?
At the beginning of your notebook, add:
%load_ext autoreload
%autoreload 2
Related
I can use the function display() to see my excel data just fine on google colabs or jupyter, but can't use it on pycharm and have to use print instead. Why is that?
Display is a module which is a part of IPython. Jupyter notebooks run the IPython kernel to execute code.
Pycharm on the other hand is just an IDE, which is completely unrelated to IPython.
In other words, the display() function isn't part of the python STL, it's just something that IPython provides, which is why you can use this function in Jupyter Notebooks.
In ipython cells you can execute shell commands as follows:
ipython:
print("asdf")
!echo asdf
However, if you try to import this code from file,
asdf.py:
def asdf():
print("asdf")
!echo asdf
ipython:
from asdf import asdf
it results in an error.
!echo asdf
^
SyntaxError: invalid syntax
The use case for that is repetative massive scripts in google colab utilizing ffmpeg, wget, mount, e.t.c.
While you can use os.system or subprocess, they are not as interactive in providing real-time stdout.
Use case solution
If you want to reuse code that has ipython magic functions in it, simply use %run instead of the import.
ipython:
%run utils.ipynb
utils.ipynb:
def asdf():
print("asdf")
!echo asdf
Example above works fine.
Taken from this answer.
On reusing ipython code
Why the original question was an A-B problem of some kind? The reason is that you should not really drag ipython additional functionality to the pure python execution.
If you have a, let's say, google colab notebook with useful utils,
then just !wget it from the public link and %run it.
If you have a python script to execute, do a regular import on .py files.
Sure, there are ways to import .ipynb files to python code: Jupyter notebook's Notebook Loader, ipynb package, nbimporter package (but even the nbimporter's author says you should not really do that. Just convert your notebook to .py using VSCode or another tool).
So, if you use ipython research environment and features, just stick with it. Or switch to pure python environment with proper modules, testing, e.t.c.
Also, if you're struggling with doing wget:
file_id = "1xE8Db1zvQ7v-z13CO2qc3F6wJmtr3YHu" # can be extracted from public link
file_out_name = "utils.ipynb"
!wget "https://docs.google.com/uc?export=download&id=""$file_id" -O "$file_out_name"
However, on the problem of calling cmd, I see no way of executing shell commands interactively in python with one line. While subprocess can do that, it takes multiple lines to achieve.
I have a set of Jupyter notebooks, all of which need the same lines of code to initialize. The initialization contains:
IPython magic commands (such as %matplotlib inline and %load_ext autoreload)
importing modules
configuring some settings, such as plot style
I am looking for a good way to put this code into a module that can be imported and ideally called with something like:
import preamble
preamble.run()
For the initialization code it is easy to do, but what about the module imports and magic commands?
You may instead use a jupyter notebook for that.
Simply put all your code into preamble.ipynb and run it in your first cell with:
%run ./preamble.ipynb
I have modified a function in a file in Spyder (and save it). Now, I rerun a cell that calls that function on my Jupyter Notebook and the modification that I made on my Spyder file does not seem to have effects on my Notebook, still mentioning an error that I had previously.
The only solution I have found to avoid this is to close the Notebook (by ctrl+C and deactivating command on Anaconda prompt and rerun the Notebook).
Of course, it's not so convenient... Is it possible to make it more efficiently ?
You can restart the kernel in jupyter instead of exiting and relaunching the app.
Then you need to re-execute the cells with the import statements.
(use restart and clear output)
There is also a jupyter magic function to reload modules documented here:
%load_ext autoreload
%autoreload 2
I want to use the useful ! call-to-shell feature of ipython in regular python script, i.e. I want the following code:
for i in range(5):
!echo {i}
To work in an in-house python module that can be imported (thus simply using ! will not work).
I've tried using subprocess but wasn't able to get the output of the shell run into the Jupyter notebook, where most of our scripts are running at.
So, what I'm looking for is using ! functionality as an IPython import some how..