ImportError: cannot import name 'app' from 'app' (app.ipynb) in jupyter notebook

Viewed 2261

Here is the directory structure

▾ 1projet/
  ▾ apps/
      __init__.ipynb
      Statistics.ipynb
      SWEDEN.ipynb
      UAE.ipynb
app.ipynb
index.ipynb

The code I wrote in index.ipynb is

import import_ipynb
from app import app
from app import server
from apps import UAE,SWEDEN,Statistics

which gives me this error

ImportError: cannot import name 'app' from 'app' (app.ipynb)

the code I wrote in app.ipynb is :

import dash

app= dash.Dash(__name__, suppress_callback_exceptions=True,
                meta_tags=[{'name': 'viewport',
                            'content': 'width=device-width, initial-scale=1.0'}]
                )
server = app.server

and the file __init__.ipynb is empty.

1 Answers

You are trying to import other .ipynb files from a .ipynb file. Please beware that this is not the same as importing a python module (.py). Here there is already an answer which might be interesting for you, there is already mentioned that all the notebooks have to be in the same directory for succesful import.

In your case you would have to install ipynb, and then import app and server with following lines:

from ipynb.fs.full.app import app
from ipynb.fs.full.app import server

In order to succesfully import the modules UAE, SWEDEN, Statistics you either have to put the .ipynb files into the same directory as index.ipynb, or you have to put the code in those files into .py files to support absolute import. In that case you would have to add __init__.py files to each folder hierachy.

In summary you have two options for your folder structure, first option:

▾ 1projet/
  __init__.py
▾ apps/
      __init__.py
      Statistics.py
      SWEDEN.py
      UAE.py
app.ipynb
index.ipynb

with imports:

from ipynb.fs.full.app import app
from ipynb.fs.full.app import server
from 1projet.apps import UAE, SWEDEN, Statistics

Second option:

app.ipynb
index.ipynb
Statistics.ipynb
SWEDEN.ipynb
UAE.ipynb

with imports:

from ipynb.fs.full.app import app
from ipynb.fs.full.app import server
from ipynb.fs.full.Statistics import *
from ipynb.fs.full.SWEDEN import *
from ipynb.fs.full.UAE import *
Related