0

I have definitions.py in the root directory (myapp/), start.py in myapp/src/scripts/. When I import definitions from start.py I am getting "ModuleNotFoundError: No module named 'definitions'". New to python, what am I missing? Thanks!

definitions.py:

#!/usr/bin/env python
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

start.py:

#!/usr/bin/env python
import definitions

if __name__ == '__main__':
    print('in start = ' + definitions.ROOT_DIR)

I did this to make it work, is there a better solution?

import sys
sys.path.insert(1, '../../')
from definitions import *
topcan5
  • 1,511
  • 8
  • 30
  • 54
  • There's lots about this topic, see e.g. https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – Demi-Lune Apr 29 '20 at 15:21
  • thanks! @Demi-Lune I edited my question, know a cleaner solution? – topcan5 Apr 29 '20 at 16:29
  • Hi, sorry I missed your question. There is in fact a better (imho) solution, involving *packaging* : setup.py, relative import and all that related stuff. See the tutorial https://packaging.python.org/tutorials/packaging-projects/ . If you're planning development in python and distributing your work, it is worth learning this. – Demi-Lune May 06 '20 at 09:19

0 Answers0