'Issue importing modules with Django, but runs fine when I run it independently using '-m' flag

Currently I have a python project with the following directory structure.

enter image description here

I can run the demo.py file directly fine (without importing it in my Django project) from the outermost directory shown above using the following command:

python -m ObjectSegmentation.segmentation_api.apps.tools.demo

However, in my Django project, I need to import the demo.py file in my django views.py file, but when I do so, I get the following error:

    from ObjectSegmentation.segmentation_api.apps.tools.test import *
ModuleNotFoundError: No module named 'ObjectSegmentation'

These are the first few lines from demo.py (including line 4) where the error occurs.

enter image description here

My installed apps in settings.py for the whole project look like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apps.tools.demo',
    'rest_framework'
]

How can I make it so that the imports work with both the project run independently and imported in my django project?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source