0

I have 3 files in a Python module called flags.

  1. Empty __init__.py file

  2. data.py

  3. lookup.py

lookup.py contains the line from data import list_of_data_items

If I then try to run

from flags import lookup

I get

ModuleNotFoundError: No module named 'data'

Importing data works without issue

>>> from flags import data
>>> 
Jonathan
  • 10,792
  • 5
  • 65
  • 85
  • can I put that in the init instead? – Jonathan Dec 19 '17 at 16:42
  • Tried both with no success. For the record I've tried importing with `import lookup` and `from lookup import *` no luck with either – Jonathan Dec 19 '17 at 16:44
  • found solution, in `lookup.py` change to the line `from .data import list_of_data_items` or `from flags.data import list_of_data_items` – Stack Dec 19 '17 at 16:48
  • That works very well. Thanks! If you put that in an answer I'll accept it. – Jonathan Dec 19 '17 at 16:49
  • Would also appreciate whatever reference you find most useful to learn more about creating Python modules. – Jonathan Dec 19 '17 at 16:50
  • I think answer is not required, you can read about relative imports here `https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time` – Stack Dec 19 '17 at 16:51
  • Great link thanks! An answer would provide a quick tldr for users who might not read the comments. – Jonathan Dec 19 '17 at 16:52

1 Answers1

1

In lookup.py change to the line from .data import list_of_data_items or from flags.data import list_of_data_items

For further reading check this link .

Stack
  • 4,116
  • 3
  • 18
  • 23