ModuleNotFoundError: No module named 'myapp'

hey, I’ve just started coding with Django but I get the following error:
from pairs.models import ContractModel
ModuleNotFoundError: No module named ‘pairs’

BASE_DIR = Path(file).resolve().parent.parent

This happens when I run the file with the python command
I searched too much but did not find the right answer.
I also checked all the paths and settings of the vs code and there are no problems.I already had several programs according to this project, which all worked correctly.
Python version: 3.11.1
Django version: 3.2.18
Pip version: Last version

Do you have a file in pairs/ to declare it as a module?


and when i use:

from .models import ContractModel
show this error:

Traceback (most recent call last):
File “E:\pairing\events\pairs\”, line 1, in
from .models import ContractModel
ImportError: attempted relative import with no known parent package

I think you may need to alter your BASE_DIR.

Can you try:
BASE_DIR = path(__file__).resolve().parent

It did not work.

Traceback (most recent call last):
File “E:\pairing\events\pairs\”, line 1, in
from pairs.models import ContractModel
ModuleNotFoundError: No module named ‘pairs’

I configured pairs/
and i can add model

from django.contrib import admin
from pairs.models import ContractModel

and i can see mymodel in Django administration:

But the error is still displayed

I noticed a new issue. I added a model in Django administration and printed the model with the following command:

python shell

from pairs.models import CotractModel
obj = ContractModel.objects.all()

QuerySet printed.
but when i want to import mymodel from pairs.models in not working thinking:

What is “”?

That is a script that I wrote before and now I want to use it in this project, but I encountered an import problem at the beginning of the work.
for example:

from pairs.models import ContractModel
cd= ContractModel.objects.all()

Ok, you can’t run Django-oriented code like that.

If you want to run Django code, using the ORM for example, you need to initialize the Django environment before doing so. What you’re trying to do here just won’t work.

If you’re looking to run Django-related code directly from the command line, the easiest way to do it is to write a custom management command.

Sorry. I don’t want to use a custom command. I did this just for testing. I used this method in my projects before and had no problems, but now my model is not imported.
Notice: finally a celery run

There is clearly something different between the code or environment that you are claiming works, and what you’ve posted here.

This file, as displayed, is not going to work.

If I call file through a view and url in my project, will my model be imported?
What way do you suggest to solve this problem?

Yes - because the Django environment will have been initialized by either the runserver or runserver_plus commands, or by something like the gunicorn wsgi container.

If you want to run that file directly from the command line, I already answered that question. Create it as a management command. The command initializes the Django environment before calling the designated command.

thank you. I will try it and come back