简体   繁体   中英

Using %run in jupyter notebook without affecting interactive namespace

I am trying to run a python script in a jupyter notebook without the variables in the script being passed to my interactive namespace after the script has been called.

An example below:

myscript.py:

var1 = 10

My notebook:

var1 = 9
%run myscript.py
print(var1)

I want the printed answer to be 9 not 10. I have also tried running %run -p myscript.py as stated in the documentation but seem to get the same result.

You aren't returning anything from the script or resetting the value of var1. This answer should help. You would need to write a function in the myscript.py that returns the value you want and then set it to var1 .

myscript.py

def this_awesome_function():
    return 10

Notebook:

from myscript import this_awesome_function

var1 = this_awesome_function()
print(var1) 

Hopefully this should work!

A colleague of mine gave the answer below which is the best one I have found so far:

import subprocess

var1 = 9
subprocess.Popen(["python3", "myscript.py"])
print(var1)

I am not sure that jupyter notebook allows you to do this. It is most likely using the %run or %prun but can't figure it out.

Not with %run presently (see bottom section); however, you don't need to run a bash script from inside your notebook or invoke subprocess . You can just send the command to a separate shell instance and run the script without affecting your local namespace using the following:

!python myscript.py

See 'Shell Commands in IPython' here and sections below that for more information. Jupyter Python notebooks build on IPython abilities.


As I advise here , usually you want the more full-featured %run in Jupyter because it handles passing back errors, input prompts, and connects the output from the script better to Jupyter notebooks in general.

So you are thinking well along the line of best practices; however, you have overlap with variables in your notebook the main portion of you script that you want to avoid that can easily be done with !python myscript.py . Normally, there wouldn't actually be much in your main namespace as you modularize your script more with functions.


I see the text here that makes you think -p option should do as you thought:

In this mode, the program's variables do NOT propagate back to the IPython interactive namespace (because they remain in the namespace where the profiler executes them).

It does seem to describe what you are saying, and I am at a loss as to explain why it doesn't. I even tried current IPython and the same thing happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM