how to import functions defined in other notebooks

27 views
Skip to first unread message

Andy Davidson

unread,
May 2, 2017, 5:33:48 PM5/2/17
to 'Ryan Blue' via Project Jupyter
I found some documentation for ipynb https://ipynb.readthedocs.io/en/latest/

I am able to import def from notebooks in the same directory how ever I can not import notebooks that are in other directories. As an example

1) my server was started on my local machine at ~/workSpace/bioInformatics

2) My first notebook is running at ~/workSpace/bioInformatics/ucsd/c2_GenomeSequencing/src

3) the name of my first notebook is week1_Introduction_to_Genome_Sequencing.ipynb

3) I want to use functions define in ../../c1_FindingHiddenMessagesinDNA/src/week4.ipynb

$ ls -l ~/workSpace/bioInformatics/ucsd/c2_GenomeSequencing/src/../../c1_FindingHiddenMessagesinDNA/src/week4.ipynb

-rw-r--r--  1 ad  staff    33K Apr 23 11:50 /Users/andrewdavidson/workSpace/bioInformatics/ucsd/c2_GenomeSequencing/src/../../c1_FindingHiddenMessagesinDNA/src/week4.ipynb

I have done a lot of hacking and keep getting ‘invalid syntax’




According to https://ipynb.readthedocs.io/en/latest/


"Relative imports

If you want to import notebooks from other notebooks relatively, you can easily do that with the following:

` import ipynb.fs from .full.notebook1 import foo from .defs.notebook2 import bar `

This will do the imports from other notebooks relative to the path of the notebook in which the importing is happening. The import ipynb.fs is boilerplate that is required for this feature to work properly.”


Any suggestions would be appreciated.

Andy

Roland Weber

unread,
May 3, 2017, 1:25:05 AM5/3/17
to Project Jupyter
Hello Andy,

relative imports doesn't mean you can navigate to parent directories.
Two suggestions:
1. Create a symbolic link (ln -s), so the other directory becomes a subdirectory.
2. Move the shared code to a library that can be used from all notebooks.

hope that helps,
  Roland

Reply all
Reply to author
Forward
0 new messages