在Dreamhost上安装Python 3

时间:2014-04-08 16:08:53

标签: python passenger dreamhost

也许我在这里遗漏了一些东西,但是我按照Dreamhost的文档中的说明来安装自定义Python解释器。我怀疑Python 2.x中的execl()存在问题 - > Python 3.x但我无法确定。

http://wiki.dreamhost.com/Python#Building_a_custom_version_of_Python

这是我遇到的问题。似乎同样的解释器会运行两次,即使我告诉它其他的东西。

import os, sys

log = file('/home/user/mysite.com/passengerwsgi.log', 'a')
log.write("Running %s\n" % (sys.executable))
log.write("Python %s\n" % (sys.version))
log.write("Path %s\n" % (sys.path))

INTERP = "/home/user/Python-3.3.5/bin/python3.3"
PACKAGES = "/home/user/mysite.com/packages"

if sys.executable != INTERP:
    log.write("Detected wrong interpreter location, swapping to %s\n" % (INTERP))
    # Swapping interpreters will not flush any files.
    log.flush()
    log.close()
    os.execl(INTERP, INERP, *sys.argv)
    # Should resume execution from the top of the file.

log.write("Loading pypiserver...")
log.flush()
sys.path.append(os.getcwd())
import site
import pypiserver
application = pypiserver.app(PACKAGES, redirect_to_fallback=False)

当它运行时,我得到了可怕的500服务器错误,但它似乎并不像我指定的插件实际上正在运行,如此输出所示。如果我指定解释器的错误路径,它会完全失败,如我所料。

> Running /usr/bin/python Python 2.6.6 (r266:84292, Dec 26 2010,
> 22:31:48) [GCC 4.4.5] Path
> ['/usr/local/dh/passenger/lib/phusion_passenger/wsgi',
> '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2',
> '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old',
> '/usr/lib/python2.6/lib-dynload',
> '/usr/local/lib/python2.6/dist-packages',
> '/usr/lib/python2.6/dist-packages',
> '/usr/lib/python2.6/dist-packages/PIL',
> '/usr/lib/pymodules/python2.6'] Detected wrong interpreter location,
> swapping to /home/user/Python-3.3.5/bin/python3.3 Running
> /usr/bin/python Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) [GCC
> 4.4.5] Path ['/usr/local/dh/passenger/lib/phusion_passenger/wsgi', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2',
> '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old',
> '/usr/lib/python2.6/lib-dynload',
> '/usr/local/lib/python2.6/dist-packages',
> '/usr/lib/python2.6/dist-packages',
> '/usr/lib/python2.6/dist-packages/PIL',
> '/usr/lib/pymodules/python2.6'] Detected wrong interpreter location,
> swapping to /home/user/Python-3.3.5/bin/python3.3

为什么两次执行/ usr / bin / python解释器?

1 个答案:

答案 0 :(得分:1)

我的passenger_wsgi.py用于启动通过pipenv管理的Flask应用。这可能对您有帮助,也可能没有帮助,但除了我如何确定运行哪个解释器之外,我看不到任何重大差异。确保您指向的路径实际上不是符号链接或启动错误python版本的脚本。

import sys, os
import subprocess

INTERP = subprocess.check_output(['pipenv', 'run', 'which', 'python3']).strip().decode('utf-8')
if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)

sys.path.append(os.getcwd())
from my_site import app

# hackish way to make Passenger urldecode the same way WSGI does
import urllib.parse
def application(environ, start_response):
    environ["PATH_INFO"] = urllib.parse.unquote(environ["PATH_INFO"])
    return app(environ, start_response)

就我的环境而言,我遵循了Dreamhost建议的约定并安装了Python 3.6.4,前缀为$HOME/opt/python-3.6.4并将$HOME/opt/python-3.6.4/bin添加到我的$PATH,并使用了Python的{{ 1}}没有额外的包装器将它放在那个地方。 make install已使用pipenv进行安装,我也在pip install --user pipenv添加了$HOME/.local/bin