ipython / ipynb

Package / Module importer for importing code from Jupyter Notebook files (.ipynb)
https://ipynb.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
245 stars 70 forks source link

investigate relative imports. #4

Open Carreau opened 8 years ago

Carreau commented 8 years ago

I'm guessing we'll have user that want to import ..anipynbmodule not sure how to do that explicitly.

yuvipanda commented 7 years ago

I played around with this. If you can get __package__ to be 'ipynb.fs' and have ipynb.fs imported, then you can just do from .somenotebook import X and it'll work same as from ipynb.fs.somenotebook import X. We could possibly do this for notebooks with an ipython extension of some sort?

yuvipanda commented 7 years ago

Right now, I tested this by having two files:

wat/say.ipynb:

def say(what):
    print(what)

wat/init.ipynb

from .say import say
__all__ = [say]

Now, from outside, I could do this:

from wat import say
say('hello world')

That calls the correct function, and works!

So this works from the outside. However, if I try to execute wat/init.ipynb in the notebook interface, I get the following error on import:

SystemError: Parent module '' not loaded, cannot perform relative import

if I run:

__package__ = 'ipynb.fs.wat'

before importing, it gets to:

SystemError: Parent module 'ipynb.fs.wat' not loaded, cannot perform relative import

Which is better. if I do a:

import ipynb.fs.wat

in init.ipynb it succeeds (weird!) and then if I try to relative import again I get:

ImportError: No module named 'ipynb.fs.wat.Untitled'; 'ipynb.fs.wat' is not a package

Curious and needs more investigation :D

yuvipanda commented 7 years ago

So I did a bunch of work here, and it kinda works.

In test.ipynb:

def say(what):
    print(what)

In client.ipynb:

__package__ = 'ipynb.fs'
import ipynb.fs
from .test import say

say("hello world")

This works. The first two lines in the client are the boilerplate. We might be able to automate that with some ipynb extension setup maybe?

Note that this totally works when you're calling it from python directly or with python -m.

yuvipanda commented 7 years ago

Funkier than expected. Right now, if notebook A imports notebook B as:

from ipynb.fs.full.notebookB import foo

and then notebook B imports notebook C as

import ipynb.fs
from .defs.notebookC import bar

The transient import will fail, since the package for notebookB will already be ipynb.fs.full.notebookB.

The underlying core problem is:

It looks like we might need to do AST munging to make this happen. We just munge AST to resolve the relative imports ourselves.