找到你要的答案

Q:Django Import Error: No module named apps

Q:Django进口错误:没有模块命名的应用程序

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.

我只是检查项目的Git。项目结构为

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

还有其他的目录和文件,但我认为那些是重要的。

当我运行服务器我得到

    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.

answer1: 回答1:

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.

Have a look at the documentation for more informations.

你需要添加一个空的__init__。PY(4总总)它被Python包在应用程序文件夹中的文件。

看看更多信息的文档。

answer2: 回答2:

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

Avoiding name clashes with built-in modules is generally advised

注意在Django 1.9有一个模块被称为django.apps

避免名称冲突与内置模块一般建议

answer3: 回答3:

If you've used django-admin startapp myapp command, it creates a apps.py file in your myapp/ folder which could be conflicting with your apps/ module folder. A "hidden" apps.pyc file could be on your myapp/ folder.

Try removing project/apps/myapp/apps.py and project/apps/myapp/apps.pyc.

如果你使用django startApp MyApp的命令,它创建了一个文件在你apps.py MyApp /文件夹可冲突与您的应用程序/模块文件夹。“隐藏”的文件可以在你apps.pyc MyApp /文件夹。

尝试删除项目/程序/ MyApp / apps.py和项目/程序/ MyApp / apps.pyc。

answer4: 回答4:

Note that in Django 1.9, you can simply add your app to INSTALLED APPS by simply putting its name, eg if your app is called "app" and you have created models in it, simply go to settings.py and then scroll to INSTALLLED APPS and add it as 'apps', that should work easily.

Note that in Django 1.9, you can simply add your app to INSTALLED APPS by simply putting its name, eg if your app is called "app" and you have created models in it, simply go to settings.py and then scroll to INSTALLLED APPS and add it as 'apps', that should work easily.

answer5: 回答5:

This can also happen if you installed your app in settings.py in your main project folder before running "python manage.py startapp [app-name]". Comment it out, create the app (should work now), then put the line back into settings.py and continue on.

这也可以如果你安装你的应用程序在您的主文件夹运行settings.py项目“python manage.py startapp [应用程序名称]”之前发生的。评论它,创建应用程序(现在应该工作),然后把线回settings.py继续。

answer6: 回答6:

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

...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 your terminal is using. On the terminal make sure to activate the virtualenv, type:

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

Confirm django is installed. If django is installed and it's still not working correctly then try to upgrade django with pip:

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

If none of above works then you know the python version is correct, your using the right python environment and manage.py is set up correctly. SO, the error is somewhere else.

首先,检查你的manage.py并确保它有正确的参考MyApp。为MyApp参考在manage.py。

应用程序/ MyApp / manage.py…

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

...

如果设置是否正确,检查Python解释器。如果你使用virtualenv,当你使用全局Python代替虚拟环境中可能会发生错误。全球使用Python可以通过设置你的IDE不当造成的,在manage.py用户错误或仓/狂欢。

确保在虚拟环境运行manage.py。检查第一道manage.py。默认情况下文件读取:

#!/usr/bin python

改变到:

#!/path/to/virtualenv/bin python

现在,检查终端和终端是Python版本使用。在终端一定要激活virtualenv,型:

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

Django安装确认。如果Django安装它仍然不能正常工作并尝试升级Django画中画:

(venv)[用户@电脑] #:pip安装——升级Django

如果没有以上的工作,那么你知道Python的版本是正确的,你使用正确的Python环境和manage.py设置正确。所以,错误就在别的地方。

python  django  module  directory  importerror