Apache2、Python3、CGI脚本

Apache2、Python3、CGI脚本,apache,python-3.x,osx-mountain-lion,homebrew,Apache,Python 3.x,Osx Mountain Lion,Homebrew,环境: Mac OS X 10.8.5 Apache2(操作系统附带的版本) Python2(操作系统附带的版本) Python3(通过自制软件安装) 此代码在web浏览器中返回“测试”: #!/usr/bin/env python # -*- coding: UTF-8 -*- from __future__ import print_function, division print("Content-Type: text/html") # HTML is following. pr

环境:

  • Mac OS X 10.8.5
  • Apache2(操作系统附带的版本)
  • Python2(操作系统附带的版本)
  • Python3(通过自制软件安装)
此代码在web浏览器中返回“测试”:

#!/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")
但此代码在web浏览器中返回“内部服务器错误”(这次使用python3):

…并在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
/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
ls-al/usr/local/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
/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
问题:

  • 既然我路径中的第一项是/usr/local/bin,为什么Apache找不到Python3
  • 如何告诉Apache使用Python3

  • 谢谢你的帮助:)

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

    #/usr/bin/python

    然而,工作:

    #/usr/local/bin/python3

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

    编辑

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

    #!/usr/local/bin/python3