部署Django在共享服务器上时找不到设置模块

0 投票
1 回答
4125 浏览
提问于 2025-04-15 20:54

我正在尝试把我的django项目部署到共享主机上,具体步骤可以参考这里

我的项目放在了 /home/user/www/testa 这个路径下

我使用了这个脚本


#!/usr/bin/python
import sys, os

sys.path.append("/home/user/bin/python")

sys.path.append('/home/user/www/testa')


os.chdir("/home/user/www/testa")

os.environ['DJANGO_SETTINGS_MODULE'] = "settings.py"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

但是当我在命令行中运行它时,出现了这个错误:


WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Traceback (most recent call last):
  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 558, in run
  File "build/bdist.linux-i686/egg/flup/server/fcgi_base.py", line 1118, in handler
  File "/home/user/lib/python2.4/site-packages/django/core/handlers/wsgi.py", line 230, in __call__
    self.load_middleware()
  File "/home/user/lib/python2.4/site-packages/django/core/handlers/base.py", line 33, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/home/user/lib/python2.4/site-packages/django/utils/functional.py", line 269, in __getattr__
    self._setup()
  File "/home/usr/lib/python2.4/site-packages/django/conf/__init__.py", line 40, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/user/lib/python2.4/site-packages/django/conf/__init__.py", line 75, in __init__
    raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'settings.py' (Is it on sys.path? Does it have syntax errors?): No module named settings.py
Content-Type: text/html



Unhandled Exception

Unhandled Exception

An unhandled exception was thrown by the application.

我哪里做错了呢?

从浏览器运行这个脚本只会给我一个内部服务器错误。

1 个回答

3

这一行

os.environ['DJANGO_SETTINGS_MODULE'] = "settings.py"

应该更像这样

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

这要根据你设置 sys.path 的方式来决定。这个环境变量应该包含模块的路径,也就是Python应该如何导入这个模块,而不是模块的实际文件名。

简单来说,你现在的设置让Django内部做了这样的事情:

import settings.py

也就是说,它试图从一个 settings 模块里面导入一个 py 模块。

撰写回答