Python Django 导入错误:没有名为应用程序的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30001009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:48:42  来源:igfitidea点击:

Django Import Error: No module named apps

pythondjangomoduledirectoryimporterror

提问by Josh

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

我刚刚用 git 检查了一个项目。项目结构是

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 checkI 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.pathand the direcory appsobviously 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显然存在。由于我作为 Python 开发人员的经验不是很丰富,因此我自己找不到解决方案。

回答by aumo

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

您需要__init__.py在文件apps夹中添加一个空文件(共 4 个下划线),以便 Python 将其识别为包。

Have a look at the documentationfor more informations.

查看文档以获取更多信息。

回答by user5909835

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

请注意,在 Django 1.9 中有一个名为 django.apps 的模块

Avoiding name clashes with built-in modules is generally advised

通常建议避免与内置模块的名称冲突

回答by Ruben

If you've used the django-admin startapp myappcommand, 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.pycfile could be in your myapp/folder.

它可能与您的apps/模块文件夹冲突。隐藏apps.pyc文件可能在您的myapp/文件夹中。

Try removing these:

尝试删除这些:

  • project/apps/myapp/apps.py
  • project/apps/myapp/apps.pyc
  • project/apps/myapp/apps.py
  • project/apps/myapp/apps.pyc

回答by mwanjajoel

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

请注意,在 中Django 1.9,您可以将您的应用添加到INSTALLED_APPS列表中。

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

如果您的应用程序名称是app并且您已经在其中创建了模型,则转到该settings.py文件并添加您的应用程序:

INSTALLED_APPS = [
    ...
    'app'
]

回答by lookininward

This can also happen if you installed your app in settings.pyin 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.pyfile and continue on.

将其注释掉,创建应用程序(现在应该可以工作),然后将该行放回settings.py文件中并继续。

回答by Diaz

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

首先,检查您的manage.py并确保它对myapp.

...apps/myapp/manage.py

...应用程序/我的应用程序/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.

如果设置正确,请检查 Python 解释器。如果您使用的是 virtualenv,则在使用全局 Python 而不是虚拟环境时可能会发生错误。使用全局 Python 可能是由于 IDE 设置不当、用户错误或manage.py.

To make sure manage.pyis 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:

现在,检查终端及其使用的 Python 版本。然后,激活 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:

确认 Django 已安装。如果是,但仍然无法正常工作,请使用以下命令对其进行升级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.pyis set up correctly, then that means the error is somewhere else.

如果即使 Python 版本正确,上述方法都不起作用,则说明您使用了正确的 Python 环境并且manage.py设置正确,则意味着错误出在其他地方。

回答by Fahri Güre??i

Command: python manage.py startapp app_name

命令: python manage.py startapp app_name

Class name must be the same as app_namein models.pyfile and then add your app_namein the INSTALLED_APPSlist in settings.py.

类名称必须相同app_namemodels.py文件,然后添加app_nameINSTALLED_APPS列表中settings.py

回答by Jai Narayan Singh

If you use this command to start an app:

如果您使用此命令启动应用程序:

django-admin startapp appexample

...then, the AppexampleConfigclass should not be listed in the settings.pyfile.

...然后,AppexampleConfig该类不应列在settings.py文件中。

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

只需appexampleINSTALLED_APPS列表中添加应用程序名称(例如)。避免使用:appexample.app.AppexampleConfig

Usually, the AppexampleConfigclass is a subclass of the django.apps.Appconfigclass 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表示 Django 应用程序及其配置的类的子类。它只是定义了 name 类属性并将其值设置为Appexample.

回答by Taylor A. Leach

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

我在跑步后解决了这个问题,python3 manage.py runserver而不是python manage.py runserver在那个问题上浪费了 25 分钟......

回答by user12225473

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 herethe first images show the wish also prompts this error that your apps are not loaded yet when you run the app.

请确保您的应用程序位于项目的根目录中。我的意思是,如果您错误地在主目录之外启动了一个应用程序,Django 将无法找到您的应用程序,唯一的解决方案是将其移动到您项目的主目录中。屏幕截图显示了如何解决问题 在此处输入图像描述第一个图像显示希望也会在您运行应用程序时提示您的应用程序尚未加载的错误。

The second screenshot shows how to fix the problem. enter image description hereThis 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.

第二个屏幕截图显示了如何解决问题。 在此处输入图像描述这就是我的意思,应用程序应位于主项目的同一目录中。就我而言,我的主目录是 crmapp。