Django Import Error: No module named apps
Django Import Error: No module named apps
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:Python27libsite-packagesdjangocoremanagement__init__.py", line 385, in execute_from_command_line
utility.execute()
File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "C:Python27libsite-packagesdjangoconf__init__.py", line 46, in __getattr__
self._setup(name)
File "C:Python27libsite-packagesdjangoconf__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "C:Python27libsite-packagesdjangoconf__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.
manage.py check
ImportError: No module named apps.
sys.path
apps
I just edited my question because it didn't tell the complete problem.
– Josh
May 2 '15 at 10:37
8 Answers
8
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 package.
__init__.py
apps
Have a look at the documentation for more informations.
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.
– rnevius
May 2 '15 at 10:31
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.
– Josh
May 2 '15 at 11:05
No, there isn't.
__init__.py
files are required to make Python treat the directories as containing packages. stackoverflow.com/questions/448271/what-is-init-py-for– rnevius
May 2 '15 at 11:08
__init__.py
Note that in Django 1.9 there is a module called django.apps
Avoiding name clashes with built-in modules is generally advised
If you've used the django-admin startapp myapp
command, it creates this file: myapp/apps.py
.
django-admin startapp myapp
myapp/apps.py
It could be conflicting with your apps/
module folder. A hidden apps.pyc
file could be in your myapp/
folder.
apps/
apps.pyc
myapp/
Try removing these:
project/apps/myapp/apps.py
project/apps/myapp/apps.pyc
Note that in Django 1.9
, you can add your app into the INSTALLED_APPS
list.
Django 1.9
INSTALLED_APPS
If your app name is app
and you have created models in it, then go to the settings.py
file and add your app:
app
settings.py
INSTALLED_APPS = [
...
'app'
]
This can also happen if you installed your app in settings.py
in your main project folder, before running this:
settings.py
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.
settings.py
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.
AppexampleConfig
settings.py
Just add the app name (e.g. appexample
) in INSTALLED_APPS
list. Avoid using: appexample.app.AppexampleConfig
.
appexample
INSTALLED_APPS
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
.
AppexampleConfig
django.apps.Appconfig
Appexample
Command: python manage.py startapp app_name
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
.
app_name
models.py
app_name
INSTALLED_APPS
settings.py
First, check your manage.py
and make sure it has the correct reference to myapp
.
manage.py
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
.
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:
manage.py
manage.py
#!/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
:
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.
manage.py
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you paste the complete stack trace of the error you get?
– mu 無
May 2 '15 at 10:12