0

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.

E_net4
  • 27,810
  • 13
  • 101
  • 139
wageeh
  • 13
  • 1
  • 5
  • 18

1 Answers1

1

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 *
Michael_A
  • 26
  • 5
  • The first importing error is fixed ! Thank you. Just to clarify for the first option do I have to change the extension to .py? And for the second option do I have to get rid of the folders and have all files in the same folder? – wageeh May 15 '21 at 15:08
  • 1
    @wageeh i am not sure if you can simply change the extension from `.ipynb` to `.py`. I think it's maybe easier to simply create the .py file with the same name and copy all code from the corresponding .ipynb file into the .py file. – Michael_A May 15 '21 at 15:13
  • 1
    @wageeh for the second option, yes you should get rid of all folders and have all files in the same folder – Michael_A May 15 '21 at 15:14
  • I went for the second option and I got this error ````ImportError: cannot import name 'Statistics' from 'ipynb.fs.full.app' (unknown location)```` – wageeh May 15 '21 at 15:19
  • sorry there was a mistake in my answer. you have to import with `from ipynb.fs.full.Statistics import *` – Michael_A May 15 '21 at 15:37