【问题标题】:Unable to load a module error in django on apache server无法在 apache 服务器上的 django 中加载模块错误
【发布时间】:2012-07-08 15:19:06
【问题描述】:

这是一件我无法理解的奇怪事情。

这是我的 django 文件目录层次结构简介

project/
       apache/ django.wsgi
       project/ __init__.py, settings.py, urls.py ..
       services/
               __init__.py
               apis/
                    __init__.py
                    fparser.py
               wservice.py
       ...

       profile.py

所以,在开发服务器甚至在 heroku (gunicorn) 上一切正常,但在 apache (localhost) 上不工作

当我打开页面时:

它的显示

Exception Type: ImportError at /
Exception Value: cannot import name website_feed_address

这个website_feed_address 位于profile.py import error 位于 fparser.py

我应该如何解决?

编辑:

django.wsgi

import os, sys

sys.path.append('d:/code/projects-dev/project')


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

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

而问题中上面提到的项目目录层次结构位于d:/code/projects-dev/

编辑 2

这些是 apache 日志错误

[Sun Jul 08 23:14:04 2012] [notice] Parent: Received restart signal -- Restarting the server.
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName
[Sun Jul 08 23:14:04 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Sun Jul 08 23:14:04 2012] [warn] mod_wsgi: Runtime using Python/2.7.2.
[Sun Jul 08 23:14:05 2012] [notice] Child 5912: Exit event signaled. Child process is ending.
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!?
[Sun Jul 08 23:14:05 2012] [notice] Apache/2.2.22 (Win32) mod_wsgi/3.3 Python/2.7.2 mod_ssl/2.2.22 OpenSSL/0.9.8t configured -- resuming normal operations
[Sun Jul 08 23:14:05 2012] [notice] Server built: Jan 28 2012 11:16:39
[Sun Jul 08 23:14:05 2012] [notice] Parent: Created child process 2120
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!?
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName
[Sun Jul 08 23:14:05 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Sun Jul 08 23:14:05 2012] [warn] mod_wsgi: Runtime using Python/2.7.2.
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!?
[Sun Jul 08 23:14:05 2012] [notice] Child 2120: Child process is running
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Acquired the start mutex.
[Sun Jul 08 23:14:06 2012] [notice] Child 5912: Released the start mutex
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting 64 worker threads.
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting thread to listen on port 443.
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting thread to listen on port 80.
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: Terminating 126 threads that failed to exit.
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: All worker threads have exited.
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: Child process is exiting

编辑 3

profile.py、fparser.py 是这样的

profile.py 只包含一些变量,元组就像settings.py。在这种情况下,它只是导入一个变量website_feed_address

这是fparser.py

from profile import website_feed_address
import feedparser


class FParser(object):
      def __init__(self):
            self.pFeed = feedparser.parse(website_feed_address)
      # rest of the code goes...

我只想在 FParser 类中使用 website_feed_address 将其作为对象参数。有没有更好的方法?...或 否则,我可以这样用吗?

【问题讨论】:

  • 如何定义 Apache 使用的 PYTHONPATH?你如何导入website_feed_address
  • @Rodrigue 以及我使用 Eclipse.. 大多数 PATH 类型的东西都由它处理。关于 django.wsgi,我在上述问题中添加了更多细节。请检查一下

标签: python django apache web-applications


【解决方案1】:

问题很可能是您在项目中使用了相对于包的导入语句,但 Apache 不知道正确的 python 路径。例如,由于您遇到了这个特定的导入错误,我假设在您的fparser.py 中您正在执行类似from profile import website_feed_address 的操作。您可以尝试在 django.wsgi 中明确包含您的项目包:

sys.path.append('d:/code/projects-dev/project')
sys.path.append('d:/code/projects-dev/project/project')

更好的建议是为您的项目设置virtualenv。这样做的好处是允许您的项目包含自己的站点包环境,以便您安装的任何依赖项都将与项目环境一起使用。然后在 Apache 中你可以告诉 mod_wsgi python 环境在哪里:

# or wherever you decide to create the virtualenv
WSGIPythonHome d:/code/projects-dev/project/project

通过 Apache 设置 pythonpath

您也可以在 Apache conf 中为您的 wsgi 应用程序尝试 setting the PYTHONPATH

WSGIPythonPath "d:/code/projects-dev/project/project"

*请注意该链接中区分 wsgi 嵌入模式和守护程序模式的地方。

解决导入问题的完全替代方法

虽然我之前的建议是解决 PYTHONPATH 并导入您的 profile 模块,但基于您只是使用它来传递常量这一事实,我建议您只使用 settings.py,因为那就是它的用途(声明常量):

settings.py

WEBSITE_FEED_ADDRESS = "http://foo.com"

fparser.py

from django.conf import settings

...
    self.pFeed = feedparser.parse(settings.WEBSITE_FEED_ADDRESS)

您可以确定 django 环境将始终为您提供设置模块。常见的方法是让应用添加到设置中,以提供默认常量。

【讨论】:

  • 将虚拟环境保存一段时间。我会认为这是最后的选择。我不知道为什么..但我只是不喜欢每次都用虚拟环境下车..
  • 我认为 virtualenv 是一个完美的入门方式。它始终确保您的项目具有包含的环境,并且您不依赖外部包。
  • 你能再看一遍问题吗..我添加了更多细节
  • 这只是确认您依赖profile 在您的pythonpath 中。如果您不想在其他地方修复 PYTHONPATH,您可能需要将其更改为 from project.profile import website_feed_address。我还添加了一些额外的信息。
  • 最糟糕的部分。我决定在构造函数中直接将地址作为字符串传递。这一次,页面没有加载......甚至没有显示错误。显示“等待本地主机”。在我的情况下,整个问题到底是什么?
【解决方案2】:

如果您将 profile.py 移动到项目文件夹中,它将位于一个包中。然后你可以这样做:

from project.profile import website_feed_address

【讨论】:

    猜你喜欢
    • 2015-05-02
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 2012-09-21
    • 2021-10-04
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多