python3 django-1.11 ImportError:没有名为'MyProject.settings'的模块

时间:2017-12-01 02:39:45

标签: python django

django 1.11

python 3.6.3

我一直在研究一个初始的django应用程序,只是将视图,模型和表单从单个文件更改为目录。我的开发机器是运行最新PyCharm的Windows机器。然后我将更新的文件“部署”到linux测试机器上。

在Linux机器上,我做了makemigrationsmigrate,这似乎有效。然后我访问了网站......

Traceback (most recent call last):
  File "/dh/passenger/helper-scripts/wsgi-loader.py", line 320, in <module>
    app_module = load_app()
  File "/dh/passenger/helper-scripts/wsgi-loader.py", line 61, in load_app
    return imp.load_source('passenger_wsgi', startup_file)
  File "/home/me/.pyenv/versions/3.5.2/lib/python3.5/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "passenger_wsgi.py", line 23, in <module>
    application = get_wsgi_application()
  File "/home/me/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/me/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/me/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/home/me/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/me/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/me/.pyenv/versions/3.5.2/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'MyProject.settings'

我已经做了相当多的谷歌搜索并看到了很多此类问题,但我还没有找到解决方案。

我测试了wsgi启动脚本,它构建/使用的所有路径都是正确有效的。该项目看起来像(为了简洁而修剪):

devSiteBase
|-- MyProject
|   |-- MyProject
|   |   |-- __init__.py
|   |   |-- settings.py
|   |   |-- urls.py
|   |   `-- wsgi.py
|   |-- Members
|   |   |-- __init__.py
|   |   |-- admin.py
|   |   |-- apps.py
|   |   |-- forms
|   |   |   |-- ...
|   |   |   |-- __init__.py
|   |   |-- models
|   |   |   |-- ...
|   |   |   |-- __init__.py
|   |   |-- tests.py
|   |   |-- urls.py
|   |   `-- views
|   |       |-- ...
|   |       |-- __init__.py
|   |-- __init__.py
|   |-- manage.py
|   `-- templates
|       |-- ...
|-- passenger_wsgi.py
|-- public
|   |-- ...

wsgi启动脚本:

import os
import sys

from django.core.wsgi import get_wsgi_application

cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(os.path.join(cwd, 'MyProject'))
sys.path.append(os.path.join(cwd, 'MyProject/MyProject'))

HOME_DIR = os.path.expanduser('~')

INTERP = os.path.join(HOME_DIR, ".pyenv/versions/3.5.2/bin/python")

if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)

sys.path.insert(0, os.path.join(HOME_DIR, '.pyenv/versions/3.5.2/bin'))
sys.path.insert(0, os.path.join(HOME_DIR, '.pyenv/versions/3.5.2/lib/python3.5/site-packages'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'MyProject.settings'

application = get_wsgi_application()

我可以运行python manage.py diffsettings,这显然可以读取设置文件。

编辑这个问题出现在Linux平台而不是PC上。

1 个答案:

答案 0 :(得分:0)

当您使用Pycharm时,您需要为您的项目右上角启用django环境,您需要选择编辑配置,您可以添加您的设置文件并启用django查看此图像它应该是这样的enter image description here