How to import variables from other .ipynb on Jupyter Notebook?

Viewed 1206

I've seen a lot of other questions here about this topic however none have helped. I am coding on Jupyter Notebook using Python 3, to organize my code I am trying to use 2 different modules for my program. Thus I have 1 module called let's for simplicity call it abc.ipynb and now created a different module called for simplicity edf.ipynb (Both on Jupyter Notebook)

At first i wasn't able to import abc to edf but after importing the import_ipynb package and fixing the PYTHONPATH it worked.

However, now I just want to import variables from abc to edf, I am trying:

  1. from abc import x
  2. import abc print(abc.x)
  3. I even tried calling a function

All give 1 of below errors:

  1. cannot import name 'x' from 'abc'
  2. module 'abc' has no attribute 'function'

Any advice? I already created an init.py in my folder however I think it is of no use since I am working with .ipynb

2 Answers

Try changing the name of the file other than "abc". Python already has a module called abc

I wanted to do the same thing and found a workaround by creating a function in a separate functions.py file that returns the variable I wanted to pass between files

from functions.py import get_variable
desired_variable = get_variable()
Related