# how to execute notebook in different namespace

**URL:** <https://discourse.jupyter.org/t/how-to-execute-notebook-in-different-namespace/33532>\
**Category:** JupyterLab\
**Created:** [March 6, 2025, 4:19pm UTC](https://discourse.jupyter.org/t/how-to-execute-notebook-in-different-namespace/33532 "2025-03-06T16:19:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![9gagmem](https://avatars.discourse-cdn.com/v4/letter/9/dfb087/32.png) [@9gagmem](https://discourse.jupyter.org/u/9gagmem)\
**Post date:** [March 6, 2025, 4:19pm UTC](https://discourse.jupyter.org/t/how-to-execute-notebook-in-different-namespace/33532/1 "2025-03-06T16:19:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [March 6, 2025, 4:53pm UTC](https://discourse.jupyter.org/t/how-to-execute-notebook-in-different-namespace/33532/2 "2025-03-06T16:53:03Z")

</div>

I believe the issue is spelled out in [the documentation for IPython magic `%run](https://ipython.readthedocs.io/en/stable/interactive/magics.html#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](https://stackoverflow.com/a/68023750/8508004).

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](https://jupytext.readthedocs.io/en/latest/using-cli.html) these days since [the developer added `--execute`](https://github.com/mwouts/jupytext/issues/231). (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.

```plaintext
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:

```plaintext
!jupytext --execute <my_jupyter_notebook.ipynb>

```

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

Also see:

- [How to run a notebook using command line](https://discourse.jupyter.org/t/how-to-run-a-notebook-using-command-line/3475)
- [python - Can I run Jupyter notebook cells in commandline? - Stack Overflow](https://stackoverflow.com/q/35471894/8508004)
- [python - How to run an .ipynb Jupyter Notebook from terminal? - Stack Overflow](https://stackoverflow.com/q/35545402/8508004)

* * *

* * *

#### Incorporated specifically into your situation

Try out what I came up with [by clicking here](https://mybinder.org/v2/gist/fomightez/6773dedf6d5132795dd4245a18c066eb/master?urlpath=git-pull%3Frepo%3Dhttps%253A%252F%252Fgist.github.com%252Ffomightez%252F978e1203a4ffbaba37e76efe6ba027af/%26urlpath%3Dlab%252Ftree%252F978e1203a4ffbaba37e76efe6ba027af/%252Fnbc.ipynb) 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`](https://nbviewer.org/urls/gist.githubusercontent.com/fomightez/978e1203a4ffbaba37e76efe6ba027af/raw/f4a0381d89042db95fd49b7aca06f5c492dc8953/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`](https://nbviewer.org/urls/gist.githubusercontent.com/fomightez/978e1203a4ffbaba37e76efe6ba027af/raw/f4a0381d89042db95fd49b7aca06f5c492dc8953/nbc.ipynb) in [this gist](https://gist.github.com/fomightez/978e1203a4ffbaba37e76efe6ba027af).

Try it out in current Jupyter without log in or signing up for anything:  
[click here for a session](https://mybinder.org/v2/gist/fomightez/6773dedf6d5132795dd4245a18c066eb/master?urlpath=git-pull%3Frepo%3Dhttps%253A%252F%252Fgist.github.com%252Ffomightez%252F978e1203a4ffbaba37e76efe6ba027af/%26urlpath%3Dlab%252Ftree%252F978e1203a4ffbaba37e76efe6ba027af/%252Fnbc.ipynb) with `nbc.ipynb` ready to be run fresh.

---

<div class="post-metadata">

**Author:** ![9gagmem](https://avatars.discourse-cdn.com/v4/letter/9/dfb087/32.png) [@9gagmem](https://discourse.jupyter.org/u/9gagmem)\
**Post date:** [March 9, 2025, 4:52pm UTC](https://discourse.jupyter.org/t/how-to-execute-notebook-in-different-namespace/33532/3 "2025-03-09T16:52:03Z")

</div>

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)

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/jupyter/original/3X/8/9/8938e38a821ff5194ea6711488f2bcb81538b8ae.png)

Then I try with GPT suggested solution:

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

```

and

```auto
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

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [March 9, 2025, 8:17pm UTC](https://discourse.jupyter.org/t/how-to-execute-notebook-in-different-namespace/33532/4 "2025-03-09T20:17:16Z")

</div>

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.
