本文介绍了Django导入错误:没有名为应用程序的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用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

运行manage.py check时得到ImportError: No module named apps.,所以我认为问题与我的设置模块无关,而与我的apps目录无关.我不确定为什么找不到我的模块应用程序,因为项目在我的sys.path上,而可怕的apps显然存在.由于我不是Python开发人员,因此我自己也找不到解决方案.

解决方案

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

有关更多信息,请参见文档. /p>

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.

解决方案

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.

这篇关于Django导入错误:没有名为应用程序的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:16