0

I'm using Python's Jupyter Notebooks, and have been trying to import a variable called thresh_mean from another file that's in the same folder.

import scipy.io as sio
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from firstsound import thresh_mean #this line

However, I get this error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-42-1ee7c2613f81> in <module>
      4 import pandas as pd
      5 import matplotlib.pyplot as plt
----> 6 import firstsound

ModuleNotFoundError: No module named 'firstsound'

Does anyone know why this is happening and how I can go about importing that variable?

NeuroKween
  • 45
  • 8
  • Where is `firstsound` installed? The problem is broader than trying to import a name from the module; the module itself can't be created. – chepner Mar 05 '20 at 20:37
  • I have a folder on my desktop. Within that folder there's a jupyter notebook called firstsound.ipynb and within the same folder is the file I'm writing. I'm trying to summon a variable from within firstsound.pynb into the current notebook. – NeuroKween Mar 05 '20 at 20:45
  • I don't use Jupyter myself; does it recognize `.pynb` files as valid modules? Python itself does not. – chepner Mar 05 '20 at 20:51
  • 1
    The extension for Jupyter notebooks is `.ipynb` and they cannot be imported the way Python can. – Wayne Mar 05 '20 at 22:20

1 Answers1

0

Since you are trying to pass a variable, you want to execute a command using %store somewhere in the source notebook after you define it, and then read it in to your other notebook by adding the -r option after %store to indicate read. See here and #9 here.

#next line run in source notebook
%store thresh_mean

#next line run in notebook trying to read the serialized variable
%store -r thresh_mean

(You may want to look into using pickle to serialize and pass variables if you are going to be using them and passing them outside of your own Jupyter environment.)

If it was a function you can use import-ipynb. There's also nbimporter.

Wayne
  • 6,607
  • 8
  • 36
  • 93