<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gaurav</title>
    <description>The latest articles on DEV Community by Gaurav (@gauravzaiswal).</description>
    <link>https://dev.to/gauravzaiswal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F525703%2F7126bfd3-6886-4602-97b2-2a937b07622b.jpeg</url>
      <title>DEV Community: Gaurav</title>
      <link>https://dev.to/gauravzaiswal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gauravzaiswal"/>
    <language>en</language>
    <item>
      <title>Two most common cause of "Django Import Error: No module named &lt;appname&gt;" in Django.</title>
      <dc:creator>Gaurav</dc:creator>
      <pubDate>Wed, 08 Dec 2021 17:22:19 +0000</pubDate>
      <link>https://dev.to/gauravzaiswal/thwo-most-common-cause-of-django-import-error-no-module-named-in-django-35h2</link>
      <guid>https://dev.to/gauravzaiswal/thwo-most-common-cause-of-django-import-error-no-module-named-in-django-35h2</guid>
      <description>&lt;p&gt;Every django developer has experience of facing this issue. It is one of the most common errors you will face when running your first migration. This error can also produce other errors such as "&lt;strong&gt;TemplateDoesNotExist&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;In this article we will see two most common reasons that cause &lt;strong&gt;Django Import Error: No module named&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Forgot to put app name to installed_apps list?
&lt;/h2&gt;

&lt;p&gt;The most common mistake that beginners make is that they forget to list their apps in INSTALLED_APPS which is present in the settings.py file.&lt;/p&gt;

&lt;p&gt;Therefore, please make sure that your app is listed in the &lt;strong&gt;INSTALLED_APPS&lt;/strong&gt;. Example:&lt;br&gt;
Suppose, I have a newly started app called &lt;strong&gt;users&lt;/strong&gt; within a project named &lt;strong&gt;polls project&lt;/strong&gt; then I would navigate to &lt;strong&gt;pollsproject/settings.py&lt;/strong&gt; and will search for INSTALLED_APPS and will append '&lt;strong&gt;users&lt;/strong&gt;' or  '&lt;strong&gt;users.apps.UsersConfig&lt;/strong&gt;' to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSTALLED_APPS = [
   'users.apps.UsersConfig', # added users app here
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the most common solution for &lt;strong&gt;TemplateDoesNotExist&lt;/strong&gt; error as well so make sure you have not committed this mistake.&lt;/p&gt;

&lt;p&gt;Now, let's look at another most common mistake that a beginner makes which results in this error.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. App is not in the root directory of your project.
&lt;/h2&gt;

&lt;p&gt;If you have listed your apps inside INSTALLED_APPS but still you are facing the *&lt;em&gt;Django Import Error: No module named *&lt;/em&gt; error then it might be due to django could not find your app within the root directory.&lt;/p&gt;

&lt;p&gt;Your apps can be place anywhere, but Django by default starts to search app from the project's root directory and if it does not find the app there then it will throw the same &lt;strong&gt;ImportError&lt;/strong&gt; error.&lt;/p&gt;

&lt;p&gt;Therefore, please make sure that your &lt;strong&gt;app is located in the same directory where manage.py file is located.&lt;/strong&gt; For example, for a random project named pollsproject, I have created an app called users, then the whole tree structure of my project would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;polls-project/
├── env/
├── pollsproject/
│   ├── pollsproject/
│   │   ├── __init__.py
│   │   ├── asgi.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── users/
│   │   ├── migrations/
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── db.sqlite3
│   └── manage.py
├── .gitignore
├── requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main cause of this problem is that when you start a fresh project and then you start an app using the django-admin command from the same location from where you run the startproject command. This will create an app outside the project's root directory. Therefore, to avoid this issue use &lt;strong&gt;python manage.py startapp  instead of  "django-admin startapp  or make sure you’re in the same directory as manage.py&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The good news is that Django's apps are "Pluggable" therefore, you can just move your app to the directory same as manage.py and it should work.&lt;/p&gt;

&lt;p&gt;Let me know if none of these two worked for you, I will be more than glad to help you.&lt;/p&gt;

&lt;p&gt;Happy Coding 🧡&lt;/p&gt;

</description>
      <category>django</category>
      <category>debug</category>
      <category>python</category>
      <category>errors</category>
    </item>
  </channel>
</rss>
