Use Django Models along with streamlit

Hi all,
I want to use Django models in my Streamlit app, but when I import the model, error says “No module named data_labelling.models”

The following is my file system
data_labelling
– models.py
data_validation
– UIDesign.py

  • both data_labelling and data_validation are apps that under a Django project.

In “UIDesign.py”, I write
from data_labelling.models import ImageLabel.
When I run streamlit run UIDesign, I get an error message says
ImportError: No module named data_labelling.models

I wonder why I see this error? and How to fix that?

Thank you!

Hi @Echo

At a glance it looks like this is unrelated to Streamlit. It’s more of a Python import system issue.

To check whether my hypothesis is right, try running UIDesign.py without Streamlit by just calling:

python UIDesign.py

…and see if you get the same ImportError in the terminal. My guess is the error will still be there.

Here’s what I think is happening: since UIDesign.py and models.py are in sibling folders, one can’t access the other unless you tell the import system to go up a level first, like this:

from ..data_labelling.models import ImageLabel

Note the .. in the import line. That’s a new Python3 feature that lets you navigate up your folder structure to find imports. (And in Python2 there’s no way to do that, so if you’re still using 2.x, you should refactor your folder structure)