1

I want to access variables defined in one ipynb file into another in jupyter notebook. My code:

mymodel.ipynb
import import_ipynb
import utils

N = 12
utils.ipynb
x, sr = librosa.load(path)
S = librosa.stft(x, N)

Error:

NameError: name 'N' is not defined

I have also tried

from utils import *
SoProgram
  • 393
  • 3
  • 13
  • Have you tried `from ipynbfile import some_variable`? It works for .py files, but I don't know the situation for `.ipynb`. – emremrah Jan 13 '20 at 17:09
  • Does this answer your question? [Share data between IPython Notebooks](https://stackoverflow.com/questions/31621414/share-data-between-ipython-notebooks) – Anand Yeole Jan 13 '20 at 17:11
  • @emremrah I have tried from filename import * – SoProgram Jan 13 '20 at 18:17

2 Answers2

0

You can use %store command.

more information can be found here and here

In source notebook

data = 'data to read'
%store data

In destination notebook

%store -r data
#now u can access data from source notebook
pppery
  • 3,731
  • 22
  • 33
  • 46
  • I need to refer various variables in various ipynb files, Are you proposing I should apply the same solution for all? – SoProgram Jan 13 '20 at 18:16
  • once you use `%store` command in source ipynb then that data can be accesed in any ipynb files so yes you can use same solution for all – Anand Yeole Jan 17 '20 at 08:38
0

It can be done in the following way,

In source jupyter notebook file, define the variables with value e.g:

userName = 'PriyabrataSamantaray'

In the other destination Jupyter notebook, call the file as below

%run /Users/priyabrata.samantaray/ConnString

Here the connString is the name of my source jupyter notebook

getusername = userName
print(getusername)

The output in the destination Jupyter notebook will be PriyabrataSamantaray This code can be used in Databricks jupyter notebook

Rahul Rahatal
  • 648
  • 5
  • 19