2

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.

1
  • My current work around is running a bash script from my jupyter notebook instead but would like to know if there is a solution using %run. Commented Feb 13, 2018 at 13:04

3 Answers 3

0

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that answer but I am specifically looking to run a python script in jupyter notebook without passing the variables back into my notebook's namespace. I could wrap the code into a function but I don't want to do that. As there must be a way to run scripts without adding variables in the script to the namespace.
If you are not passing the variables back, then using the %run magic will work, but your myscript.py isn't doing anything. Essentially, you are creating a new variable var1 inside myscript.py then when the script ends it is being destroyed without anything happening to it. If you included a print() statement within myscript.py then it might print something out.
Just to clarify, I do not want myscript.py to change the value of var1 in my notebook, but from the example given in the question it does. When you %run a python script the variables in that script are passed back into your notebooks namespace (see here) - I do not want that to happen. I want the script's namespace to be destroyed. The documentation suggests that using the -p flag will achieve what I am trying to do but I have had no luck with it so I might be implementing it incorrectly.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.