0

In a website I'm building with Flask I've got structure like this:

├── app
│   ├── __init__.py
│   ├── models.py
│   ├── ticket_scanner
│   │   ├── __init__.py
│   │   └── filehelper.py
│   ├── templates
│   │   └── all the templates are here..
│   └── views.py
└── run.py

I now want to import models into ticket_scanner/__init__.py. I tried to do this using the following import statements (with the errors behind them):

from .. import models  # ImportError: cannot import name models
from . import models  # ImportError: cannot import name models
import models  # ImportError: No module named models
from ..models import ImageHash  # ImportError: cannot import name ImageHash

As you can see it refuses to import from the models.py file. I do have circular imports in these files, but I wouldn't know where it goes wrong. Below this message I pasted the full stacktrace of my first attempt (from .. import models).

Does anybody know what I'm dong wrong here? All tips are welcome!

Traceback (most recent call last):
  File "./run.py", line 4, in <module>
    from app import app
  File "/Users/kramer65/repos/fts/app/__init__.py", line 16, in <module>
    import views, models
  File "/Users/kramer65/repos/fts/app/views.py", line 5, in <module>
    from models import Ticket
  File "/Users/kramer65/repos/fts/app/models.py", line 8, in <module>
    from ticket_scanner import filehelper, pdf as pdf_helper, util
  File "/Users/kramer65/repos/fts/app/ticket_scanner/__init__.py", line 8, in <module>
    from .. import models
ImportError: cannot import name models
kramer65
  • 50,427
  • 120
  • 308
  • 488

2 Answers2

-1

I am not sure whether relative imports like these work at all. But in my projects, I put a simple setup.py in the top directory that installs the entire project. Then you can do python setup.py develop and import from the project.

Also I never put any code into the __init__.py. I import everything in the scripts. Not sure if that would ease your problem.

P.R.
  • 3,785
  • 1
  • 27
  • 47
-1

I think if you use from app import models or from app.models import ImageHash

Ariel Infante
  • 41
  • 1
  • 4