0

提前感谢我的查询。

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

对于 Heroku 部署 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 行,在 execute_from_command_line(sys.argv) 文件“/Library/Python/2.7/ site-packages/django/core/management/ init .py”,第 363 行,在 execute_from_command_line 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 (name)文件“/Library/Python/2.7/site -packages/django/conf/初始化.py”,第 41 行,在 _setup self._wrapped = Settings(settings_module) 文件“/Library/Python/2.7/site-packages/django/conf/init .py”,第 110 行,在init mod = importlib.import_module ( self.SETTINGS_MODULE)文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init .py”,第 37 行,在 import_module 导入名称) ImportError:没有名为 src 的模块。推文设置

***这是我的包结构:***附加的 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

4

1 回答 1

0

如果manage.py不在src目录中,则需要添加src到 Python 路径。

import sys
sys.path.append('src')

然后你应该保持DJANGO_SETTINGS_MODULE原样,没有src.

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tweetme.settings")
于 2018-01-04T12:22:54.803 回答