mange.py中的Django settings.py导入错误配置错误

时间:2018-01-04 12:18:10

标签: django heroku django-forms django-rest-framework heroku-api

先谢谢我的查询。

我创建了一个django项目,它在src文件夹中有manage.py文件,从SRC文件夹运行终端时工作正常。

对于Heroku deployement manage.py应该放在根目录中,所以我用“os.environ.setdefault(”DJANGO_SETTINGS_MODULE“,” src.tweetme.settings “)更新了manage.py文件。”

将“os.environ.setdefault(”DJANGO_SETTINGS_MODULE“,” tweetme.settings “)更改为”os.environ.setdefault“(”DJANGO_SETTINGS_MODULE“,” src.tweetme.settings “)”在本地运行时面临导入问题。需要解决方法如何在两个文件夹深处导入setting.py。

#!/usr/bin/env python

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.tweetme.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

我正面临以下错误: 附加了Error_Message.png

Hiras-Mac-mini-2:tweethere apple $ python manage.py runserver Traceback(最近一次调用最后一次):   文件“manage.py”,第23行,in     execute_from_command_line(sys.argv中)   在execute_from_command_line中输入文件“/Library/Python/2.7/site-packages/django/core/management/init.py”,第363行     utility.execute()   文件“/Library/Python/2.7/site-packages/django/core/management/init.py”,第307行,执行     settings.INSTALLED_APPS   文件“/Library/Python/2.7/site-packages/django/conf/init.py”,第56行, getattr     self._setup(名称)   文件“/Library/Python/2.7/site-packages/django/conf/init.py”,第41行,在_setup中     self._wrapped =设置(settings_module)   文件“/Library/Python/2.7/site-packages/django/conf/init.py”,第110行, init     mod = importlib.import_module(self.SETTINGS_MODULE)   在import_module中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py”,第37行     导入(名称) ImportError:没有名为src.tweetme.settings

的模块

***这是我的包结构:***附带Project_Structure.png

-Project
    -bin
    -include
    -lib
    -src
       -subProject1
       -subProject2
       -subProject3
       -mainProject
             -Settings
                 -__init__.py
                 -base.py
                 -local.py
                 -production.py
       -manage.py
       -db.sqlite3
    -static-serve
       -.env
       -.python
       -manage.py
       -ProcFile
      -requirements.txt

Project_Structure.png Error_Message.png

1 个答案:

答案 0 :(得分:0)

如果src不在src目录中,则需要将import sys sys.path.append('src') 添加到Python路径中。

DJANGO_SETTINGS_MODULE

然后,您应该保持src不变,而不是os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tweetme.settings")

{{1}}