Attempted relative import with no known parent package

When my debugger execute this line of code from .models import Expense,Category from views.py 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 :

views.py and models.py are in same directory . And also the directory has init.py file .

some of the code of views.py 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':
        search_str=json.loads(request.body).get('searchText')
        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)
        data=expenses.values()
        return JsonResponse(list(data),safe=False)

And also some of the code of models.py file :

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

class Expense(models.Model):
    
    amount=models.FloatField()
    date=models.DateField(default=now)
    description=models.TextField()
    owner=models.ForeignKey(to=User,on_delete=models.CASCADE)
    category=models.CharField(max_length=266)
    

    def __str__(self):
        return self.category
    
    class Meta:
        ordering:['-date']
        
class Category(models.Model):
        name=models.CharField(max_length=255)
        class Meta:
            verbose_name_plural='Category'
        def __str__(self):
            return self.name

How can I solve this issue ?

Are you running this within the context of the manage.py 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.)

1 Like

Oh ! I got it . Thanks a lot .

Hello Gurus,
my structure as below
.
|_ models.py
|_collectInfo.py (Manually Created for some purpose)

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

(vPy3Env) amritendudas@amritendudas-mac stocks_api % python collectInfo.py
/Users/amritendudas/vPy3Env/myDjProjects/stocksTest/stocks_api
Traceback (most recent call last):
  File "collectInfo.py", 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.

2 Likes

Bro i spent a day to solve this problem. But i solved the problem with your a line answer. Thank you very much

I was trying to run it through the IDE’s run. Tanks

Eu estava tentando executar pelo ‘run’ da IDE.