Attempted relative import with no known parent package

When my debugger execute this line of code from .models import Expense,Category from file then it shows an error . The error is :

Exception has occurred: ImportError
attempted relative import with no known parent package
for better understanding about the error , see this image : and are in same directory . And also the directory has file .

some of the code of file :

from django.shortcuts import render,redirect
from .models import Expense,Category
from django.contrib import messages
from django.core.paginator import Paginator
import json
from django.http import JsonResponse
def search_expenses(request):
    if request.method=='POST':
        expenses=Expense.objects.filter(           amount__istartswith=search_str,owner=request.user)|Expense.objects.filter(date__istartswith=search_str,owner=request.user)|Expense.objects.filter(description__icontains=search_str,owner=request.user)|Expense.objects.filter(category__icontains=search_str,owner=request.user)
        return JsonResponse(list(data),safe=False)

And also some of the code of file :

from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now

class Expense(models.Model):

    def __str__(self):
        return self.category
    class Meta:
class Category(models.Model):
        class Meta:
        def __str__(self):

How can I solve this issue ?

Are you running this within the context of the runserver command, or are you trying to run this module directly?

(This error is typically caused by running code directly from within a package instead of from the parent system.)


Oh ! I got it . Thanks a lot .

Hello Gurus,
my structure as below
| (Manually Created for some purpose)

i was trying to access class created inside from, and i was manually running getting the below error

(vPy3Env) amritendudas@amritendudas-mac stocks_api % python
Traceback (most recent call last):
  File "", line 14, in <module>
    from .models import nseStock
ImportError: attempted relative import with no known parent package
(vPy3Env) amritendudas@amritendudas-mac stocks_api %

Is this a Django command? You can’t just run Python scripts using Django models.

If you want a Python script to have access to your models, see Writing custom django-admin commands.