[image] thelpend: Yes, it’s exactly what i’m trying to do. So when you run this for the 10th time, you want 10 copies of that same data in the database? If you’re doing it with 100 users, you want 1000 entries in your database? These entries don’t go away when the app ends. The way you’re de… Answer from KenWhitesell on forum.djangoproject.com
🌐
Django Forum
forum.djangoproject.com β€Ί using django
relative import with no known parent package - Using Django - Django Forum
June 9, 2021 - [Newbie] Good evening, I’m a newbie using python I just created a project in Django and I want to save a basic record as the first and last name of an employee. I created a β€œseed.py” file within my β€œemployee” applicati…
🌐
Django Forum
forum.djangoproject.com β€Ί using django β€Ί mystery errors
Attempted relative import with no known parent package
March 2, 2021 - 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 .
Discussions

Django tutorial: ImportError: attempted relative import with no known parent package - Stack Overflow
The "from django.urls import path" line runs fine, but the "from . import views" line gives me the following error: ImportError: attempted relative import with no known parent package More on stackoverflow.com
🌐 stackoverflow.com
attempted relative import with no known parent package
  1. Look into management commands. That's probably the easiest way to actually load up an instance of your server, so that you can load up the models alongside your script.

  2. I'm having a bit of trouble following what you're trying to do. Normally, when creating an api, you're trying to serve something; thus is the whole point of django (it's just a series of requests/responses). Most of the time, you can use an API client (httpie, thunder, rapidapi, postman, etc.) to test the endpoints you create, however, I don't know what you have in your client.py file so I don't know the context for your project scope.

More on reddit.com
🌐 r/django
3
1
September 25, 2023
apps.py: Attempted relative import with no known parent package
Attempting to run migrate on a fresh pip install in Django 1.10.7, I got the error File "/Users/me/.virtualenvs/proto/lib/python3.6/site-packages/django_q/apps.py", line 3, in from .conf i... More on github.com
🌐 github.com
5
July 26, 2017
ImportError: attempted relative import with no known parent package : Forums : PythonAnywhere
Doing relative imports like that will not work when the file that you're running is the top level file, because there is nothing for the import to be relative to. When you run that file as part of the Django app, the parent package will be defined and the import should work (that is assuming ... More on pythonanywhere.com
🌐 pythonanywhere.com
June 7, 2020
🌐
Reddit
reddit.com β€Ί r/django β€Ί attempted relative import with no known parent package
r/django on Reddit: attempted relative import with no known parent package
September 25, 2023 -
  1. This is probably a python problem,but i guess lot of people on this sub would have experience with this.
    I have a python script 'client.py' which is supposed to fetch some data from an API,i want to save this data as my model fields .but I'm getting 'attempted relative import with no known parent package' when i try to import the models module .also if i try using an absolute import i get the 'module doesnt exist' error.
    In the event of trial and error ,i also noticed that i get this error even from importing models to my views or even to admin.py, which shouldn't be a problem, since ive done this several times in my previous projects.
    if i run the server, i have no problem,but the error rises when running just the script(its probably something basic that im missing i guess).

2. also is what im doing a good idea? im trying to fetch data from an api.put it into a model then serialize it again, to replicate the initial API, just to practice how to serve APIs.

is there a better way to go about it,maybe like deserializing it without using a model?i thought about that,but i just started learning django,so i dont wanna rush.

Thanks a lot

my file path
-project
    -project_api
        migrations
        _init_.py
        admin.py
        apps.py
        models.py
        tests.py
        client.py
        views.py
   -project
        _init_.py
        asgi.py
        wsgi.py
        settings.py
    db.sqlite3
    manage.py

🌐
Quora
quora.com β€Ί How-do-I-solve-ImportError-attempted-relative-import-with-no-known-parent-package-I-am-trying-to-import-a-function-inside-my-Django-project-but-getting-the-issue-of-ImportError-attempted-relative-import-with-no
How do I solve ImportError: attempted relative import with no known parent package? I am trying to import a function inside my Django pro...
Answer (1 of 2): What is your file structure (directories and files and don’t forget the __init__.py files that exist), and which module is the one that is executing? What is the exact import statement you are trying to execute ? Also what exact exist scripts that you execute ?
🌐
GitHub
github.com β€Ί Koed00 β€Ί django-q β€Ί issues β€Ί 249
apps.py: Attempted relative import with no known parent package Β· Issue #249 Β· Koed00/django-q
July 26, 2017 - Attempting to run migrate on a fresh pip install in Django 1.10.7, I got the error File "/Users/me/.virtualenvs/proto/lib/python3.6/site-packages/django_q/apps.py", line 3, in from .conf import Conf ImportError: attempted relati...
Author: Koed00
Find elsewhere
🌐
PythonAnywhere
pythonanywhere.com β€Ί forums β€Ί topic β€Ί 27707
ImportError: attempted relative import with no known parent package : Forums : PythonAnywhere
June 7, 2020 - Doing relative imports like that will not work when the file that you're running is the top level file, because there is nothing for the import to be relative to. When you run that file as part of the Django app, the parent package will be defined and the import should work (that is assuming that the models that you are trying to import are defined in /home/ptesol/ptesolpro/ptesolapp/models.py)
🌐
DEV Community
dev.to β€Ί crawler β€Ί importerror-attempted-relative-import-with-no-known-parent-package-2je6
ImportError: attempted relative import with no known parent package - DEV Community
May 1, 2024 - The error message below occurs when you use the incorrect virtual environment interpreter that you created for your Django project. To resolve this issue, follow the steps outlined in this instructional video.
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 62092116 β€Ί importerror-attempted-relative-import-with-no-known-parent-package-when-running
python - ImportError: attempted relative import with no known parent package when running main.py - Stack Overflow
[Running] python -u "g:\Study\Python\Django Project\checkproject\checkapp\views.py" Traceback (most recent call last): File "g:\Study\Python\Django Project\checkproject\checkapp\views.py", line 3, in <module> from .models import registerPerson ImportError: attempted relative import with no known parent package
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί how-to-fix-importerror-attempted-relative-import-with-no-known-parent-package
How to Fix: ImportError: attempted relative import with no known parent package - GeeksforGeeks
July 23, 2025 - The "ImportError: attempted relative import with no known parent package" error typically arises in Python projects with multiple modules or packages. Python uses the concept of packages and modules to organize code into manageable units.
🌐
Stack Overflow
stackoverflow.com β€Ί questions β€Ί 62894432 β€Ί attempted-relative-import-with-no-known-parent-package
python - "Attempted relative import with no known parent package" - Stack Overflow
from django.urls import path from . import views urlpatterns = [ path('index/', views.index, name='main-view') ] I get an error : "ImportError: attempted relative import with no known parent package" What should I do with it? python Β· django Β· Share Β· Improve this question Β·
🌐
Code with Mosh
forum.codewithmosh.com β€Ί django
When i do relative import following error coming - Django - Code with Mosh Forum
November 15, 2021 - ImportError: attempted relative import with no known parent package ModuleNotFoundError: No module named β€˜views’ please give me suggestion
🌐
Django Forum
forum.djangoproject.com β€Ί using django β€Ί getting started
Import Error: attempted relative import with known parent package - Getting Started - Django Forum
August 28, 2023 - Making my first app with tutorial on django have this issue. How can I fix it? I try to import views from the same directory and have Import Error
🌐
Python Pool
pythonpool.com β€Ί home β€Ί error β€Ί fix importerror: attempted relative import with no known parent package
Fix ImportError: Attempted Relative Import With No Known Parent Package
July 13, 2026 - Fix attempted relative import errors by running modules with -m, using a real package layout, checking __package__, and avoiding fragile script execution paths.
🌐
Reddit
reddit.com β€Ί r/djangolearning β€Ί django + docker-compose: attempted relative import with no known parent package
r/djangolearning on Reddit: Django + docker-compose: attempted relative import with no known parent package
September 6, 2023 -

I'm woking on a project with requires a celery worker to perform article scraping, but I'm having trouble with accessing certain models from inside the django project. Specifically, I'm thrown the error "ImportError: attempted relative import with no known parent package". Celery works as intended when I'm not trying relative imports.

Here is the tree for my project (simplified):

.
β”œβ”€β”€ celeryworker
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ celeryconfig.py
β”‚   β”œβ”€β”€ celerytasks.py
β”‚   └── requirements.txt
└── newspigeon
    β”œβ”€β”€ Dockerfile
    β”œβ”€β”€ docker-compose
    β”œβ”€β”€ home
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ admin.py
    β”‚   β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”‚   └── __pycache__
    β”‚   β”œβ”€β”€ models.py #(TRYING TO TAKE MODELS FROM HERE!)
    β”‚   β”œβ”€β”€ tasks.py
    β”‚   β”œβ”€β”€ tests.py
    β”‚   β”œβ”€β”€ urls.py
    β”‚   └── views.py

The error comes from the following line in celerytasks.py:

from ..newspigeon.home.models import NewsArticle, nlp_models

In my understanding, this import error is common in the python realm, but I'm wondering if there is a django-friendly fix, meaning I don't have to alter the file structure drastically or do anything with altering sys or os, which introduce other complications.

I'm wondering if anyone has experience in dealing with relative import errors in django.

Thanks and let me know if I'm being unclear/am missing key pieces of code.

Top answer
1 of 7
47

It seems, from Python docs and experimenting, that relative imports (involving ., .. etc) only work if

  1. the importing module has a __name__ other than __main__, and further,
  2. the __name__ of the importing module is pkg.module_name, i.e., it has to be imported from above in the directory hierarchy (to have a parent pkg as part of it's __name__.)

OR

the importing module is being specified via module syntax that includes a parent pkg as python -m pkg.module, in which case it's __name__ is still __main__, so it is being run as a script, yet relative imports will work. Here __package__ is set and used to find the parent package while __name__ is __main__; more here.

[After all that, it appears that __package__ and sys.path are key to determining if/how relative imports work. __name__ indicates script or module(i.e., __main__ or module_name). __package__ indicates where in the package the relative imports occur with respect to, and the top of __package__ needs to be in sys.path.]

So, continuing with @AmitTendulkar 's example, if you run this as > python main.py or > python -m main or > python -m ecommerce.products from the project root directory, or enter interactive python from that root directory and import main, or import ecommerce.products the relative imports in products.py will work.

But if you > python products.py or > python -m products from within ecommerce directory, or enter interactive python from that ecommerce directory and import products they will fail.

It is helpful to add

print("In module products __package__, __name__ ==", __package__, __name__)

etc. in each file to debug.

UPDATE:

How imports work depend on sys.path and __package__, not on __name__. Issued from /home/jj, > python sub/mod.py has a sys.path, __package__ of /home/jj/sub, None -absolute imports of modules in sys.path work, relative imports fail.

> python -m sub.mod has sys.path, __package__ of /home/jj, sub -absolute imports of modules in sys.path work, relative imports work relative to sys.path + __package__.

It is more helpful to add

import sys    
print("In module products sys.path[0], __package__ ==", sys.path[0], __package__)

etc. in each file to debug.

2 of 7
22

i had a similar issue on Windows, and what helped me was (adapted to your directory):

# local imports
import sys
sys.path.append(r"C:\path\to\your\project")

from ecommerce.database import Database
🌐
GitHub
gist.github.com β€Ί sashadev-sky β€Ί d60a50dd2179d05bafcfa4a8d9c0d88c
"ImportError: attempted relative import with no known parent package" and its sibling "ValueError: attempted relative import beyond top-level package" Β· GitHub
If the module's name does not contain any package information (e.g., it is set to __main__), then relative imports are resolved as if the module were a top-level module, regardless of where the module is actually located on the file system.
🌐
Napuzba Tutorials
napuzba.com β€Ί home β€Ί python β€Ί 3 best solutions for β€œimporterror: attempted relative import with no known parent package”
3 Best Solutions For "ImportError: attempted relative import with no known parent package"
If __name__ variable does not have any package information, the python interpreter should treat the module as a top-level module (a module that does not belong to any package). In that case, the current package does not exist. Therefore, the python interpreter can not resolve the location of the imported module and you get β€œImportError: attempted relative import with no known parent packageβ€œ
Published: October 13, 2024
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί importerror: attempted relative import with no known parent package
r/learnpython on Reddit: ImportError: attempted relative import with no known parent package
July 9, 2022 -

Hi all!

Say I have this folderstructure:

package
β”œβ”€β”€ __init__.py
β”œβ”€β”€ module.py
└── subpackage
    β”œβ”€β”€ bar.py
    β”œβ”€β”€ __init__.py
    └── subsubpackage
        β”œβ”€β”€ __init__.py
        └── foo.py

And I would like to import a function called 'func' from 'bar.py' into 'foo.py. How would I do this?

I tried literally every combination possible...like:

from .bar import func

from subpackage.bar import func

from ..bar import func

from . import bar.func

Problem is....nothing works, as I always get this error:

ImportError: attempted relative import with no known parent package

When I read on stackoverflow, I read a lot of different answers, most of them involving manipulating sys.path and quite complex.

I dont really understand why such a simple thing would be so complicated...what am I missing here?