# ModuleNotFoundError: No module named 'myapp'

**URL:** <https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248>\
**Category:** Mystery Errors\
**Created:** [August 21, 2023, 2:53pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248 "2023-08-21T14:53:24Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 2:53pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/1 "2023-08-21T14:53:24Z")

</div>

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

> Blockquote  
> INSTALLED\_APPS = [  
> ‘django.contrib.admin’,  
> ‘django.contrib.auth’,  
> ‘django.contrib.contenttypes’,  
> ‘django.contrib.sessions’,  
> ‘django.contrib.messages’,  
> ‘django.contrib.staticfiles’,  
> ‘pairs.apps.PairsConfig’,  
> ]  
> BASE\_DIR = Path( **file** ).resolve().parent.parent

This happens when I run the listing.py file with the python listing.py command  
 ![3](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/1102a1853748fe58c5f48d05e88f608b362e490a.png)  
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

---

<div class="post-metadata">

**Author:** ![RDWE](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rdwe/32/15495_2.png) [@RDWE](https://forum.djangoproject.com/u/RDWE)\
**Post date:** [August 21, 2023, 3:02pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/2 "2023-08-21T15:02:50Z")

</div>

Do you have a ` __init__.py` file in `pairs/` to declare it as a module?

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 3:23pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/3 "2023-08-21T15:23:09Z")

</div>

yes,

 ![۱](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/e/e1caecc23428542e109d7afeb5f030979519a6fa.png)

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 3:38pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/4 "2023-08-21T15:38:51Z")

</div>

and when i use:

from .models import ContractModel  
in listing.py  
show this error:

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

---

<div class="post-metadata">

**Author:** ![RDWE](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rdwe/32/15495_2.png) [@RDWE](https://forum.djangoproject.com/u/RDWE)\
**Post date:** [August 21, 2023, 4:11pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/5 "2023-08-21T16:11:27Z")

</div>

I think you may need to alter your `BASE_DIR`.

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

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 5:17pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/6 "2023-08-21T17:17:37Z")

</div>

It did not work.  
Error:

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

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 5:48pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/7 "2023-08-21T17:48:04Z")

</div>

I configured pairs/admin.py:  
and i can add model

> Blockquote  
> from django.contrib import admin  
> from pairs.models import ContractModel  
> admin.site.register(ContractModel)

and i can see mymodel in Django administration:

 ![4](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/19756fb6aab663f60dd9b09749f0b9151a474dba.png)

But the error is still displayed

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 7:31pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/8 "2023-08-21T19:31:22Z")

</div>

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

> python manage.py shell

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

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 21, 2023, 7:56pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/9 "2023-08-21T19:56:22Z")

</div>

What is “listing.py”?

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 8:02pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/10 "2023-08-21T20:02:46Z")

</div>

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()  
> print(cd)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 21, 2023, 8:08pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/11 "2023-08-21T20:08:43Z")

</div>

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](https://docs.djangoproject.com/en/4.2/howto/custom-management-commands/).

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 8:15pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/12 "2023-08-21T20:15:45Z")

</div>

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 listing.py

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 21, 2023, 8:24pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/13 "2023-08-21T20:24:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 8:27pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/14 "2023-08-21T20:27:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 21, 2023, 8:50pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/15 "2023-08-21T20:50:36Z")

</div>

> [@fatirtatlis](#):
>
> If I call listing.py file through a view and url in my project, will my model be imported?

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 `manage.py` command initializes the Django environment before calling the designated command.

---

<div class="post-metadata">

**Author:** ![fatirtatlis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fatirtatlis/32/15491_2.png) [@fatirtatlis](https://forum.djangoproject.com/u/fatirtatlis)\
**Post date:** [August 21, 2023, 8:53pm UTC](https://forum.djangoproject.com/t/modulenotfounderror-no-module-named-myapp/23248/16 "2023-08-21T20:53:35Z")

</div>

thank you. I will try it and come back
