-1

I have imported functions before successfully, but the following has left me confused. Maybe, I just don't know enough about importing in python (I have tested the following principle on my own machine (Mac) and found that it works. I created fome functions with nothing but print statements and called them exactly the same way.).

I am running a script (train.py) that import functions from a file in the /src/models/models.py. Using from src.models.models import * /src and train.py are in the same directory.

However, everytime I run python train.py I get ModuleNotFoundError: No module named 'src'.

(source of the script github) https://github.com/aildnont/covid-cxr/blob/master/src/train.py

Same directory hierarchy and syntax works on my own computer (again I used some simple functions with print statements). I am running train.py on a linux remote, using Python 3 on both machines. Any idea? Thanks!

Amartya Barua
  • 155
  • 1
  • 12

1 Answers1

1

Use relative imports:

from models.models import *
Victor
  • 2,864
  • 1
  • 12
  • 20
  • I got "No module named '__main__.models' : '__main__' is not a package. – Amartya Barua Apr 19 '20 at 22:14
  • Try it without the trailing dot `from models.models import *` – Victor Apr 19 '20 at 22:17
  • That worked! Why didn't the absolute import work, but the relative import did? I would love to know if you have figured it out. Thanks! – Amartya Barua Apr 19 '20 at 22:31
  • Check out https://stackoverflow.com/questions/448271/what-is-init-py-for and http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html – Victor Apr 19 '20 at 22:37