Skip to content Skip to sidebar Skip to footer

Pass Variable From Jupyter Notebook To Python Script

I would like to define a variable in a Jupyter notebook and then pass it to a python script. For example, in the notebook: a = [1, 2, 3, 4] %run example.py print foo And in examp

Solution 1:

The python script cannot see the variables in jupyter notebook environment because they are different programs. When you use magic commands it runs as a separate program. If you want to pass the variable to the method then you have the option to convert that into a function. Say add

def add(a):
  b = [5, 8, 9, 10]
  foo = a + b

Then in Jupyter you can do this

from example import addadd(a)

You have to bring them in the same program. Otherwise you can pass the arguments and parse them in the script using argv. But I don't think you want to do that as you want them to see each other.

Post a Comment for "Pass Variable From Jupyter Notebook To Python Script"