【问题标题】:django on cherrypy: running server.py within eclipse is OK, but not in a terminalCherrypy 上的 django:在 Eclipse 中运行 server.py 可以,但不能在终端中运行
【发布时间】:2014-09-03 13:30:36
【问题描述】:

我按照教程 Tango with Django 构建了我的 Django 项目。一切运行正常。

文件结构:

tango/
  rango/
  tango/
    wsgi.py
    settings.py
  manage.py

现在我正在尝试在 CherryPy 服务器上部署项目,遵循this tutorialwsgi.py的默认内容如下:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

现在在与wsgi.py 相同的文件夹中,我创建了server.py

from wsgi import application
import cherrypy

if __name__ == '__main__':
    # Mount the application
    cherrypy.tree.graft(application, "/")
    # Unsubscribe the default server
    cherrypy.server.unsubscribe()
    # Instantiate a new server object
    serve = cherrypy._cpserver.Server()

    # Configure the server object
    server.socket_host = "0.0.0.0"
    server.socket_port = 8080
    server.thread_pool = 30
    # Subscribe this server
    server.subscribe()
    # Start the server engine (Option 1 *and* 2)
    cherrypy.engine.start()
    cherrypy.engine.block()

我的问题:

如果我在 Eclipse 中运行 server.py(右键单击 server.py --> 运行方式 --> Python 运行),一切正常。但是,如果我在终端中输入命令$ python server.py,则会显示以下错误消息:

Traceback (most recent call last):
  File "server.py", line 1, in <module>
    from wsgi import application
  File "<tangoProject>/tango/wsgi.py", line 14, in <module>
    application = get_wsgi_application()
  File "<virtualenv>/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "<virtualenv>/local/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'tango.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named tango.settings

注意,上面我用&lt;djangoProject&gt;&lt;virtualenv&gt;分别指定了项目和virtualenv的目录。

似乎服务器找不到tango/settings.py 文件。我该如何解决?

【问题讨论】:

  • 您是否已将路径添加到sys.path

标签: django cherrypy


【解决方案1】:

在你的server.py从wsgi导入之前添加:

import sys
sys.path.append('/the/path/to/your/project')

然后,从wsgi导入的那一行改成:

from tango.wsgi import application

【讨论】:

  • 成功了,谢谢。但是,将server.py 移动到项目文件夹(tango/)而不是应用程序文件夹(tango/tango/),然后修改from tango.wsgi import application 这一行不是更好吗?
  • 是否更好取决于您。就我而言,我会照你说的做。添加到 sys 路径的好处是server.py 可以位于任何地方。
猜你喜欢
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
相关资源
最近更新 更多