how to execute notebook in different namespace

Let say I have two notebooks:

  • nba.ipynb
  • nbb.ipynb

In notebook A, I try to run: %run -n nbB.ipynb so that the two notebooks are running in two different namespace. However, after execute %run -n nbB.ipynb, it still overwrite on my x, y variable

Sample code:
nba.ipynb

x = 2

y = 100
%store y

%run -n nbb.ipynb
%store -r y   # it still update y regardless having this or not

nbb.ipynb

x=3

%store -r y
y += 50

How to fix this?

I believe the issue is spelled out in the documentation for IPython magic `%run.
It says towards the bottom of that big section:

“There is one special usage for which the text above doesn’t apply: if the filename ends with .ipy[nb], the file is run as ipython script, just as if the commands were written on IPython prompt.”

I think that means in the case of running in Jupyter, then it is run in the current notebook’s namespace and the flags listed prior to that that you could use, such as -p to run off in the profiler namespace are not an option, although that doesn’t seem to work even with scripts as it sounds, at least to me, as I puzzled over in the past.

Fortunately, this is easily solved other ways using approaches you’d use to run notebooks from the command line. Because it is a very useful and convenient to have in your Jupyter toolbox, I like to use Jupytext these days since the developer added --execute. (Install it first with conda install conda-forge::jupytext or pip install jupytext depending on your package management.)
Then to execute a notebook on the command line you’d run.

jupytext --execute <my_jupyter_notebook.ipynb>

To translate that to running from inside an active notebook, you just add the exclamation point in front of that command to tell Jupyter to run it off in an isolated process:

!jupytext --execute <my_jupyter_notebook.ipynb>

I am going to adapt this to your provided situation below:

Also see:



Incorporated specifically into your situation

Try out what I came up with by clicking here to launch an active Jupyter session with the new nbc.ipynb that I came up with, along with versions of nbb.ipynb and nba.ipynb.

Details
I think the content in this new version of nba.ipynb, nbc.ipynb adapts my suggestions to your provided example. In the course of working it out I expanded on your provided code and you can see what I expect to accompany the new nbc.ipynb in this gist.

Try it out in current Jupyter without log in or signing up for anything:
click here for a session with nbc.ipynb ready to be run fresh.

1 Like

Hell @fomightez : thank you for your help and my apologize that I could not reply earlier

I tried your solution but encounter the following issue (sorry since I can only post one media, 1st cell follow your solution and 2nd, 3rd and 4th after trying to fix issues in 1st cell)

Then I try with GPT suggested solution:

import os
os.environ['PYDEVD_DISABLE_FILE_VALIDATION'] = '1'

and

import asyncio

# Close the current event loop if it exists to avoid errors
try:
    asyncio.get_event_loop().close()
except:
    pass

# Create and set a new event loop
asyncio.set_event_loop(asyncio.new_event_loop())

But there is still some issue remain as in cell 4th

If you happens to know how to fix this, I really appreciate

Thank you

A warning is just a warning you can read about that separate issue that most likely doesn’t matter elsewhere in this forum. The important thing is the notebook you executed to run with jupytext seems to run both times.

Given that the notebook executed I am not understanding the issue here? Please explain it in text here.

Or another way to look into this…
Did you try nbc.ipynb where I supplied a place to try it under 'Try it out in current Jupyter '? Did you try your version of nbb.ipynb there?


Minor thing but related:
By the way, screenshots are not a way to share code and steps here or on forums like this. Please describe what you did and what you saw that was not as expected in the text of the post. If you need to share a notebook, you can use code sharing resources.