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
Related
I can't import statsmodels.api in Jupyter Notebook anymore. I thought that it requires updating statsmodels.api. Then I typed "Conda update statsmodels.api". Then, the message comes up below.
PackageNotInstalledError: Package is not installed in prefix.
prefix: XXX
package name: statsmodels.api
Note: you may need to restart the kernel to use updated packages.
In order to update statsmodels.api, it seems that it would require restarting the kernel. But when trying to restart kernel, the warning came up as below.
"Do you want to restart the current kernel? All variables will be lost."
What does "all variable will be lost" mean? Will I lose all the things saved at Jupyter notebook? If so, how can I restart the Kernel safely without losing all the things I keep in my Jupyter notebook?
Restarting your kernel will reset your Jupyter notebook and remove all variables or methods you have defined.
You will not lose the code written by you. Just that, you have to run all the code cell again to set the variables and methods.
OR,
You can do "Restart & Run All"
It will show the message--
Are you sure you want to restart the current kernel and re-execute the whole notebook? All variables and outputs will be lost.
However, after selecting the above option, all the variables and methods will be set again. You don't have to manually execute all the code cells.
Jupyter lab has this feature where I can have a ipython console for every notebook I have opened. Whenever I run a cell inside this notebook, the console will have all the variables defined and modules imported corresponding to notebook. In addition, we can run extra commands and helps in debugging at times. Is there a similar feature in VS code? I really like it and would like to move completely to vs code. Python interactive command line in vscode is the closest to this that I found. However, it is not attached to the notebook and I have to run all the code inside the notebook which is a bit tedious.
I believe this would work Connecting a terminal to an existing kernel
However, you're likely looking for a way to do this within VS code. You might be able to do this by running %connect_info in a cell, starting a terminal, and then running the appropriate jupyter command.
Something like so:
jupyter console --existing kernel-2c0993da-95c7-435a-9140-118c10d33e1a.json
If you're refering to .py files you can do that the same way you would in pycharm.
First, you need to put a breakpoint in the code:
Them you run the code with the debugger:
Then, when the code reaches the breakpoint, you will be able to play with the variables, like the Jupyter terminal:
I also like to have a JupyterLab-style console open that is connected to a notebook. This is my workaround in order to achieve this in Visual Studio Code (at least it works when my kernel is a remote Jupyter session).
Suppose your notebook is called hello.ipynb.
Create a dummy file called hello.py.
Open hello.py, right-click in the code window and choose Run Current File in Interactive Window. This opens the JupyterLab-style console.
Change the kernel for the interactive window to the same kernel that the notebook hello.ipynb is using.
(Optional) Close the hello.py tab since it is not needed.
Now I have an interactive window sharing everything with the notebook.
Currently, I write pythons files in Vim, and run it with jupyter qtconsole. The advantage of this way is that I could work with Vim so get all the benefits of Vim.
I could run the python directly in Vim using the pymode plugins, but in this way I cannot see and manipulate the output variables, and the figures are opened in another window which is quite annoying when I have to close them to make Vim responsible again. Compared with this, in jupyter qtconsole I could use %maplotlib inline to display figures elegantly.
However, my current workflow has a big disadvantage that every time I run my python script in qtconsole, and then I edit my python script, it is not so easy to run it again with the modified script. Since the module has been loaded, rerun it will not automatically reload the modified module source. I found no easy way to overcome this drawback. Currently I have to restart the kernel and then reset the path, turn on %matplotlib inline, and %run python-script.py again.
Any one can give me a solution?
I find an answer which solves my problem by using ipython extension autoreload.
%load_ext autoreload
%autoreload 2
Then I do not have to restart kernel any more.
I use Jupyter Notebook to run a series of experiments that take some time.
Certain cells take way too much time to execute so it's normal that I'd like to close the browser tab and come back later. But when I do the kernel interrupts running.
I guess there is a workaround for this but I can't find it
The simplest workaround to this seems to be the built-in cell magic %%capture:
%%capture output
# Time-consuming code here
Save, close tab, come back later. The output is now stored in the output variable:
output.show()
This will show all interim print results as well as the plain or rich output cell.
TL;DR:
Code doesn't stop on tab closes, but the output can no longer find the current browser session and loses data on how it's supposed to be displayed, causing it to throw out all new output received until the code finishes that was running when the tab closed.
Long Version:
Unfortunately, this isn't implemented (Nov 24th). If there's a workaround, I can't find it either. (Still looking, will update with news.) There is a workaround that saves output then reprints it, but won't work if code is still running in that notebook. An alternative would be to have a second notebook that you can get the output in.
I also need this functionality, and for the same reason. The kernel doesn't shut down or interrupt on tab closes. And the code doesn't stop running when you close a tab. The warning given is exactly correct, "The kernel is busy, outputs may be lost."
Running
import time
a = 0
while a < 100:
a+=1
print(a)
time.sleep(1)
in one box, then closing the tab, opening it up again, and then running
print(a)
from another box will cause it to hang until the 100 seconds have finished and the code completes, then it will print 100.
When a tab is closed, when you return, the python process will be in the same state you left it (when the last save completed). That was their intended behavior, and what they should have been more clear about in their documentation. The output from the run code actually gets sent to the browser upon reopening it, (lost the reference that explains this,) so hacks like the one in this comment will work as it can receive those and just throw them into some cell.
Output is kind of only saved in an accessible way through the endpoint connection. They've been working on this for a while (before Jupyter), although I cannot find the current bug in the Jupyter repository (this one references it, but is not it).
The only general workaround seems to be finding a computer you can always leave on, and leaving that on the page while it runs, then remote in or rely on autosave to be able to access it elsewhere. This is a bad way to do it, but unfortunately, the way I have to for now.
Related questions:
Closed IPython Notebook that was running code
Confirms that output will not be updated, but does not mention the interrupt functionality.
IPython Notebook - Keep printing to notebook output after closing browser
Offers a workaround in a link. Referenced above
First, install
runipy
pip install runipy
And now run your notebook in the background with the below command:
nohup runipy YourNotebook.ipynb OutputNotebook.ipynb >> notebook.log &
now the output file will be saved and also you can see the logs while running with:
tail -f notebook.log
I am struggling with this issue as well for some time now.
My workaround was to write all my logs to a file, so that when my browser closes (indeed when a lot of logs come through browser it hangs up too) I can see the kernel job process by opening the log file (the log file can be open using Jupyter too).
#!/usr/bin/python
import time
import datetime
import logging
logger = logging.getLogger()
def setup_file_logger(log_file):
hdlr = logging.FileHandler(log_file)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
def log(message):
#outputs to Jupyter console
print('{} {}'.format(datetime.datetime.now(), message))
#outputs to file
logger.info(message)
setup_file_logger('out.log')
for i in range(10000):
log('Doing hard work here i=' + str(i))
log('Taking a nap now...')
time.sleep(1000)
With JupyterLab:
This is not a problem if you are using JupyterLab (with current release v3.x.x).
To be more specific, not a problem means that, after we close the tab/browser, the notebook's kernel is kept running (so long as the jupyter server/your terminal is not closed). But the printing output of the cell (if there is any) is interrupted.
So, when we reopen the notebook, variables and etc. are all kept and updated, except the interrupted printing output.
If you care about the printing info in this case, you could try to logging it to a file. OR try using Jupyter's execute API (see below).
With Jupyter Notebook:
If you are still sticking with legacy (e.g. version 5.x/6.x) Jupyter Notebook, well, it is still not possible in the past (i.e prior to 2022).
BUT, with the planned new Notebook v7 release, by reusing the the JupyterLab codebase, this problem will also be solved in the new Jupyter Notebook.
So, try using JupyterLab or wait and updating to Notebook v7:
$ jupyter lab --version
$ 3.4.4
$ # OR waite and update the notebook, untill
$ # make sure the installed version of notebook is v7
$ jupyter notebook --version
$ 6.4.12
With Jupyter's execute API:
Other workaround is by using Jupyter's execute API:
$ jupyter nbconvert --to notebook --execute mynotebook.ipynb
This is like running the notebook as a .py file, i.e. from the command line, not a web browser UI mode.
After its execution, a new file named mynotebook.nbconvert.ipynb will be produced, and all printing output will be kept in it, but all variables will be lost. What we could do is pickling the variables that we care about.
And I don't think using runipy is still a good choice, since it's deprecated and unmaintained (after Jupyter's execute API).
ref:
Q: is it possible to make a jupyter notebook run even if the page is closed?
A: This is being solved in JupyterLab and will be solved in the future Notebook v7 release.
If you've set all cells to run and want to periodically check what's being printed, the following code would be a better option than %%capture. You can always open up the log file while kernel is busy.
import sys
sys.stdout = open("my_log.txt", "a")
I've constructed this awhile ago using jupyter nbconvert, essentially running a notebook in the background without any UI:
nohup jupyter nbconvert --ExecutePreprocessor.timeout=-1 --CodeFoldingPreprocessor.remove_folded_code=False --ExecutePreprocessor.allow_errors=True --ExecutePreprocessor.kernel_name=python3 --execute --to notebook --inplace ~/mynotebook.ipynb > ~/stdout.log 2> ~/stderr.log &
timeout=-1 no time out
remove_folded_code=False if you have Codefolding extension enabled
allow_errors=True ignore errored cells and continue running the notebook to the end
kernel_name if you have multiple kernels, check with jupyter kernelspec list
I was wondering if there is a way to restart the ipython kernel without closing it, like the kernel restart function that exists in the notebook. I tried %reset but that doesn't seem to clear the imports.
Even though it would be handy if %reset would clear the namespace and the cache for the imports (as in the notebook) one can explicitly reload a previously imported module using importlib.reload in python3.4 or imp.reload in python3.0-3.3 (and if needed reset the kernel in a second step).
I could restart the kernel, but some console sessions take longer to reconnect. Notebook detects kernel restart instantly.
ipykernel.ipkernel.IPythonKernel class has a do_shutdown method with restart parameter which defaults to False.
Get a reference to ipykernel.kernelapp.IPKernelApp which has a reference to the kernel and call do_shutdown of the kernel by passing True.
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)
How did I test?
$ #start notebook
$ jupyter notebook
$ #connect to existing kernel
$ jupyter console --existing
If you have installed Spyder with anaconda, then open Spyder window.
Then Consoles (menu bar) -> Restart Consoles.
or you can use CTRL+. which is a shortcut key to restart the console.
I personaly use add these two lines at the top of each ipynb file in JupyterLab:
load_ext autoreload
%autoreload 2
It allows you to update the code in an adjacent xxx.py file, without having to restart the Kernel, which was a huge painpoint for me.
In the qt console you could hit ctrl-
IPython Qt-console has a reset kernel feature. You could use that if you are using IPython Qt. IMO it is better than using from the shell.
In 3.7 Anaconda, just go to Kernel, select Restart