Django Import Error: No module named apps
Asked Answered
P

21

28

I just checked out a project with git. The project structure is

project
  apps
    myapp
      settings
        __init__.py
      __init__.py
    manage.py

There are other directories and files, but I think those are the important ones.

When I run the server I get

    Traceback (most recent call last):
  File "C:/Dev/project/apps/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'apps.myapp.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named apps.myapp.settings

When running manage.py check I get ImportError: No module named apps. so I guess the problem has nothing to do with my setting module but with my apps directory. I'm not sure why it can't find my module apps, because project is on my sys.path and the direcory apps obviously exists. As I'm not very experienced as a Python developer I don't find a solution myself.

Pundit answered 2/5, 2015 at 10:10 Comment(3)
Can you paste the complete stack trace of the error you get?Feigin
I just edited my question because it didn't tell the complete problem.Pundit
I was having an almost similar problem, but my error was ModuleNotFoundError: No module named 'app.Apps' so when I searched that using Google, it led me to this question. My problem was because I used capital A for "Apps". Spent a good 30 minutes so yeah, check your spelling if this happened to you too.Ulund
G
25

You need to add an empty __init__.py (4 underscores in total) file in the apps folder for it to be recognized by Python as a package.

Have a look at the documentation for more informations.

Gouveia answered 2/5, 2015 at 10:16 Comment(3)
And just a note to the OP: Use two sets of two underscores. The question posted only has singles, while this answer has the correct number.Biramous
This works, but every project this project is depending on has the same structure and I can't change those projects. As those projects are running at the production server there must be something else I can do without creating a file.Pundit
No, there isn't. __init__.py files are required to make Python treat the directories as containing packages. https://mcmap.net/q/21646/-what-is-__init__-py-forBiramous
M
14

This can also happen if you installed your app in settings.py in your main project folder, before running this:

python manage.py startapp [app-name]

Comment it out, create the app (should work now), then put the line back into the settings.py file and continue on.

Masha answered 20/8, 2016 at 15:21 Comment(1)
You were correct, if the app name is already added to the settings.py the python manage.py startapp would throw Module not found errorCoauthor
B
11

Note that in Django 1.9 there is a module called django.apps

Avoiding name clashes with built-in modules is generally advised

Banns answered 10/2, 2016 at 17:51 Comment(0)
B
11

If you've used the django-admin startapp myapp command, it creates this file: myapp/apps.py.

It could be conflicting with your apps/ module folder. A hidden apps.pyc file could be in your myapp/ folder.

Try removing these:

  • project/apps/myapp/apps.py
  • project/apps/myapp/apps.pyc
Bucella answered 9/3, 2016 at 17:13 Comment(1)
what if i want to use an apps subdirectory and keep the myapp/apps.py file also? is there no way to explicitly indicate where you want to import from with from apps.myapp import ... ? django recommends using apps.py in an app directory for subclassing AppConfig but this conflicts with an apps directory very easily. i guess i'll just use config.py instead of apps.pyPendergast
A
7

It might be a comma missing after your config path:

  INSTALLED_APPS = [
    'palc.apps.PalcConfig', # This comma
    'django.contrib.admin',
     ...]
Aorist answered 20/9, 2021 at 13:40 Comment(0)
R
3

Note that in Django 1.9, you can add your app into the INSTALLED_APPS list.

If your app name is app and you have created models in it, then go to the settings.py file and add your app:

INSTALLED_APPS = [
    ...
    'app'
]
Ravenravening answered 8/5, 2016 at 15:52 Comment(0)
B
2

First, check your manage.py and make sure it has the correct reference to myapp.

...apps/myapp/manage.py

...
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
...

If the settings are correct, check the Python interpreter. If you're using virtualenv, errors can occur when you use the global Python instead of the virtual environment. The use of global Python can be caused by improperly setting up your IDE, user error or incorrect bin/bash in manage.py.

To make sure manage.py is running on the virtual env, check the first line in manage.py. By default the file reads:

#!/usr/bin python

Change that to:

#!/path/to/virtualenv/bin python

Now, check the Terminal and what Python version it's using. Then, activate the virtualenv:

[user@pc] #: source /path/to/virtualenv/bin/activate 
(venv) [user@pc] #: pip list

Confirm Django is installed. If it is and it's still not working correctly, then upgrade it with pip:

(venv) [user@pc] #: pip install --upgrade django

If none of the above works even though the Python version is correct, you're using the right Python environment and manage.py is set up correctly, then that means the error is somewhere else.

Barometrograph answered 5/10, 2016 at 0:59 Comment(0)
M
1

For people who are here becuase adding a new view and bumped into this problem I did the above desceribed by aumo For me worked adding a empty init.py inside my folder view and inside the folder I was adding eg.

-- example-folder

  --   views
  -- `__init.py__`
  -- `index.py`

-- `__init.py__`
-- `urls.py`

hope makes sense

Metamer answered 26/11, 2020 at 18:8 Comment(0)
P
0

Command: python manage.py startapp app_name

Class name must be the same as app_name in models.py file and then add your app_name in the INSTALLED_APPS list in settings.py.

Poverty answered 5/10, 2017 at 20:11 Comment(0)
S
0

If you use this command to start an app:

django-admin startapp appexample

...then, the AppexampleConfig class should not be listed in the settings.py file.

Just add the app name (e.g. appexample) in INSTALLED_APPS list. Avoid using: appexample.app.AppexampleConfig.

Usually, the AppexampleConfig class is a subclass of the django.apps.Appconfig class that represents a Django application and its configuration. It just defines the name class attribute and sets its value to Appexample.

Sidle answered 15/1, 2018 at 11:33 Comment(0)
E
0

I got past this issue after running python3 manage.py runserver instead of python manage.py runserver Wasted a solid 25 minutes on that one...

Edwinedwina answered 1/1, 2019 at 4:28 Comment(0)
M
0

Please make that your app is in the root directory of your project. By this I mean if by mistake you start an app outside your main directory, Django will not be able to find your app and the only solution is to move it to your project's main directory. The screenshots show how to fix the problem enter image description here the first images show the wish also prompts this error that your apps are not loaded yet when you run the app.

The second screenshot shows how to fix the problem. enter image description here This is what I mean that the app should be in the same directory of the main project. In my case my main directory is crmapp.

Murtha answered 16/10, 2019 at 9:12 Comment(0)
N
0

I had exactly the same issue. In settings.py I printed the SYS path by:

  import sys
  print(sys.path)

It turned out that main path to the project directory was not included, so I had to add it manually here.

  import sys
  sys.path.append(<path to my main project dir>)
Necessitous answered 23/12, 2019 at 8:15 Comment(0)
T
0

Had a similar issue with Django 3.1.5.

Solved it by deleting all __pycache__ folders in the project.

Telemeter answered 30/1, 2021 at 11:27 Comment(0)
F
0

another possible cause for this is if the Django app in question was created somewhere 'outside' the project root. E.g., executing:

project_root/manage.py startapp search

, I didn't realize that my Django app was created in the current folder, not inside the project root (in my case, named just that, 'project_root', haha). Django conventions dictate that the app, referenced in this case by 'search' in INSTALLED_APPS inside settings.py, should be on the BASE_DIR (project_root in my case).

Fionnula answered 1/6, 2021 at 17:2 Comment(0)
C
0

if you are using Docker for deploying your application with default command like:

uwsgi --socket=0.0.0.0:9000 --module=myapp.wsgi:application --py-autoreload=1

make sure to change your working directory in the end of Dockerfile like:

WORKDIR /absolute/path/to/source/code

Carob answered 15/11, 2021 at 11:38 Comment(0)
A
0

Go to the virtual enviroment and upgrade your django using command pip install --upgrade Django

Azarria answered 4/1, 2022 at 17:36 Comment(0)
R
0

Make sure you first execute python manage.py startapp app_name and then add app_name into settings.py

Resolve answered 26/4, 2022 at 16:50 Comment(0)
F
0

When executing (in TERMINAL)

python manage.py startapp (your_app_name)

First check apps.py in (your_app_name) folder.

It should be something like this:

class (__auto_generated__)Config(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = '(your_app_name')

Second check settings.py, in INSTALLED_APPS[''], you must have 'your_app_name'

Be careful when you change your app names

Fatherhood answered 21/8, 2022 at 11:0 Comment(0)
R
0

I had the same issue and I fixed it by adding sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) to manage.py file:

def main():
    """Run administrative tasks."""
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    # ⬇️⬇️⬇️ HERE ⬇️⬇️⬇️    
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    execute_from_command_line(sys.argv)

Check complete description of the issue here

Robertson answered 20/9, 2023 at 14:3 Comment(0)
S
-1

On my side, I have forgotten the comma (',')

INSTALLED_APPS = [
    ...
    'app',
]
Scandalmonger answered 4/1, 2023 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.