ImportError:无法导入设置 - 没有模块命名设置

时间:2015-04-01 22:22:22

标签: django

我有一个看似基本的路径问题,但我无法解决这个问题。

我有以下目录结构:

    └── rockitt
        ├── activities
        │   ├── migrations
        │   ├── templates
        │   │   └── activities
        │   └── templatetags
        ├── blog
        │   ├── settings

blog/settings内我有: base.py dev.py __init.py__

上面目录中的

__init.py__包含: from .dev import *

运行manage.py之类的东西时,我收到以下错误:

ImportError: Could not import settings 'blog.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings

到目前为止我尝试了什么:

  1. 我已经检查了manage.py正在运行时存在哪些路径,以下路径位于列表顶部: rockitt lib/python2.7/Django-1.7.7-py2.7.egg lib/python2.7 [other dirs...] ENV/lib/python27.zip ENV/lib/python2.7 ENV/lib/python2.7/plat-linux2 ENV/lib/python2.7/lib-tk ENV/lib/python2.7/lib-old ENV/lib/python2.7/lib-dynload /usr/local/lib/python2.7 /usr/local/lib/python2.7/plat-linux2 /usr/local/lib/python2.7/lib-tk ENV/lib/python2.7/site-packages

  2. 我试图手动加载设置文件而没有运气: 从我试过的博客/设置目录(我不确定这是否是基于阅读this的测试方法的正确方法:

    >>> import dev
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "dev.py", line 1, in <module>
        from .base import *
    ValueError: Attempted relative import in non-package
    
  3. 我有点坚持这个。我 - = think = - 它也可能与我的wsgi.py文件有关(见下文)。

    更多信息:

    1. wsgi.py:

      import os, sys, site
      site.addsitedir('/home/thisUserName/webapps/dev_django_rockitt/ENV/lib/python2.7/site-packages')
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog.settings.dev")
      activate_this =     os.path.expanduser("/home/thisUserName/webapps/dev_django_rockitt/ENV/bin/activate_this.py")
      execfile(activate_this, dict(__file__=activate_this))
      
      #calculate the path based on the location of the WSGI script
      project = '/home/thisUserName/webapps/dev_django_rockitt/rockitt'
      workspace = os.path.dirname(project)
      sys.path.append(workspace)
      sys.path = ['/home/thisUserName/webapps/dev_django_rockitt/rockitt/blog', '/home/thisUserName/webapps/dev_django_rockitt/rockitt'$
      
      from django.core.wsgi import get_wsgi_application
      application = get_wsgi_application()
      
    2. 堆栈跟踪:

      Traceback (most recent call last):
        File "manage.py", line 10, in <module>
      execute_from_command_line(sys.argv)
        File "/home/thisUserName/webapps/dev_django_rockitt/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/management/__init__.py", line 385, in execute_from_command_line
      utility.execute()
        File "/home/thisUserName/webapps/dev_django_rockitt/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/management/__init__.py", line 345, in execute
      settings.INSTALLED_APPS
        File "/home/thisUserName/webapps/dev_django_rockitt/lib/python2.7/Django-1.7.7-py2.7.egg/django/conf/__init__.py", line 46, in __getattr__
      self._setup(name)
        File "/home/thisUserName/webapps/dev_django_rockitt/lib/python2.7/Django-1.7.7-py2.7.egg/django/conf/__init__.py", line 42, in _setup
      self._wrapped = Settings(settings_module)
        File "/home/thisUserName/webapps/dev_django_rockitt/lib/python2.7/Django-1.7.7-py2.7.egg/django/conf/__init__.py", line 98, in __init__
      % (self.SETTINGS_MODULE, e)
      ImportError: Could not import settings 'blog.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings
      

1 个答案:

答案 0 :(得分:7)

由于我自己的愚蠢而删除这个问题几乎是诱人的,但我认为这个答案可能会帮助其他正在编码并且缺乏睡眠的人。

真的很简单 - 这是blog/settings目录中的拼写错误。

我输入了__init.py__,但显然它应该是__init__.py

一旦纠正,错误消失了。

我希望这有助于将来的某些人。 Facepalm时刻。