安装和运行 psycopg2 + Windows + Apache2 + mod_wsgi 的问题

1 投票
1 回答
707 浏览
提问于 2025-04-15 18:37

1) 我正在尝试设置一个新的网络环境来运行 Python 和 psycopg2 的代码。以下是我的步骤:

2) 下载 这个文件

3) 把下载的 mod_wsgi-win32-ap22py26-3.0.so 文件复制到 C:\Program Files\Apache Software Foundation\Apache2.2\modules 文件夹里,并把它重命名为 mod_wsgi.so

接下来,在 C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf 文件中添加以下新行:

LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias /wsgi/ "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/wsgi/"

4) 创建一个名为 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\wsgi\myapp.py 的文件,并写入以下内容:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

5) 通过访问 http://localhost/wsgi/myapp.py 来查看结果

6) 安装 这个安装包

7) 如果我把文件内容修改为:

import psycopg2

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

我会遇到以下错误:

ImportError: No module named psycopg2

我该如何告诉 Apache,我已经在 C:\Python26 中安装了 psycopg2 模块呢?

8) 我运行了以下独立脚本来确认 psycopg2 已经安装。

import psycopg2

print "Hello, World!"

我用以下方式运行它:

C:\Documents and Settings\yan-cheng.cheok\Desktop>mypython.py
Hello, World!

看起来我的 Python 环境是正常的。

1 个回答

1

我通过把Python脚本移到htdocs文件夹外面,解决了这个问题。

WSGIScriptAlias /wsgi "C:/wsgi/"

<Directory "C:/wsgi">
    AllowOverride None
    Options None
    Order deny,allow
    Allow from all
</Directory>

撰写回答