【问题标题】:Apache2, Python3, CGI ScriptApache2、Python3、CGI 脚本
【发布时间】:2014-05-31 16:20:28
【问题描述】:

环境:

  • Mac OS X 10.8.5
  • Apache2(操作系统自带的版本)
  • Python2(操作系统自带的版本)
  • Python3(通过 Homebrew 安装)

此代码在网络浏览器中返回“测试”:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from __future__ import print_function, division

print("Content-Type: text/html")  # HTML is following.
print()  # Blank line, end of headers.

print("testing")

但是这段代码在网络浏览器中返回“Internal Server Error”(这次使用python3):

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

print("Content-Type: text/html")  # HTML is following.
print()  # Blank line, end of headers.

print("testing")

...在 Apache2 错误日志中:

env: python3: No such file or directory
Premature end of script headers: test_cgi.py

echo $PATH:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

看得更深,ls -al /usr/bin/python*

/usr/bin/python
/usr/bin/python-config
/usr/bin/python2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5
/usr/bin/python2.5-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5-config
/usr/bin/python2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
/usr/bin/python2.6-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6-config
/usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
/usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
/usr/bin/pythonw
/usr/bin/pythonw2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/pythonw2.5
/usr/bin/pythonw2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw2.6
/usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

ls -al /usr/local/bin/python*:

/usr/local/bin/python3 -> ../Cellar/python3/3.3.3/bin/python3
/usr/local/bin/python3.3 -> ../Cellar/python3/3.3.3/bin/python3.3
/usr/local/bin/python3.3-config -> ../Cellar/python3/3.3.3/bin/python3.3-config
/usr/local/bin/pythonw3.3 -> ../Cellar/python3/3.3.3/bin/pythonw3.3

问题:

  1. 既然我的 PATH 中的第一项是 /usr/local/bin,为什么 Apache 找不到 Python3?
  2. 如何让 Apache 使用 Python3?

感谢您的帮助:)

【问题讨论】:

    标签: apache python-3.x osx-mountain-lion homebrew


    【解决方案1】:

    我也有同样的问题;我不知道如何让 apache 识别 python3。此外,如果您手动指定解释器:

    #!/usr/bin/python

    但是有效:

    #!/usr/local/bin/python3

    在 apache 错误日志中抱怨“来自脚本的标头格式错误”...我不知道为什么这是一个问题,因为它应该只是运行任意解释器。

    编辑

    好的,所以我的问题实际上只是我没有输出标题信息。您的问题是没有设置 python3 环境变量。尝试将第一行更改为:

    #!/usr/local/bin/python3
    

    【讨论】: