Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+2 votes

I understand that importing a notebook into another one is done by

from notebook_name import *

If there are a lot of notebooks in a given project and I create subdirectories to organise them, is there a way to import notebooks from subdirectories instead of manually changing the working directory each time? Some variations like

from directory/notebook_name import *

did not work. Is there a simple way to do this?

in General questions by (610 points)

1 Answer

+1 vote

Do this as you do with any Python module which is stored in a subdirectory: use a "dot" as path separator. So

from directory.notebook_name import *
by (70.9k points)

Oh I see. And what if I need to go one up one down?

Let's say I have a project directory which contains two directories dir1 and dir2 which contain module1 and module2 respectively. How do I import module2 into module1?

I would advise to avoid a structure like that. It's probably cleaner to make dir2 a subdirectory of dir1 if module1 depends on module2.

If you insist though, you can add the parent directory to the path and then import as usual, e.g.

import sys
sys.path.insert(0,'..')

after which you can do, in module1,

from dir2 import *
...