如何使用WSGI在virtualenv中运行Python?

时间:2014-05-04 17:06:27

标签: python apache virtualenv

我正在尝试使用WSGI和Apache在沙箱中运行Python。

  1. 我创建了虚拟环境:

    virtualenv /var/www/demo-environment --python /usr/bin/python3.3
    
  2. 我还创建了以下/var/www/demo.py文件:

    #!/usr/bin/env python
    
    import sys
    
    def application(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return "Running " + str(sys.version_info)
    
  3. 最后,我更改了Apache配置:

    WSGIPythonPath /var/www/demo-environment/lib/python3.3/site-packages/
    WSGIDaemonProcess example.com python-path=/var/www/demo-environment/lib/python3.3/site-packages/
    WSGIProcessGroup example.com
    WSGIScriptAlias / /var/www/demo.py
    
  4. 当访问网站的主页时,它显示以下内容:Running sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0),表示虽然Python有效,但它不会在虚拟环境中调用。

    由于这是启用virtualenv的错误方法,这是正确的吗?

1 个答案:

答案 0 :(得分:1)

只需使用设置WSGIPythonHome指定env的根目录:

WSGIPythonHome /home/grapsus/work/python/env
WSGIScriptAlias /demo /home/grapsus/work/python/demo.py

我修改了您的脚本以打印sys.path

Running ['/home/grapsus/work/python/env/lib/python2.7', ...