2

I'm trying to run Python within a sandbox using WSGI and Apache.

  1. I created the virtual environment:

    virtualenv /var/www/demo-environment --python /usr/bin/python3.3
    
  2. I also created the following /var/www/demo.py file:

    #!/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. Finally, I changed Apache configuration like this:

    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
    

When going to the home page of the website, it shows the following contents: Running sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0), indicating that while Python works, it is not called within a virtual environment.

Since this is the wrong way to enable virtualenv, which is the right one?

1 Answer 1

1

Just use the setting WSGIPythonHome to specify the root of your env:

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

I modified your script to print sys.path:

Running ['/home/grapsus/work/python/env/lib/python2.7', ...
Sign up to request clarification or add additional context in comments.

3 Comments

With WSGIPythonHome, Apache doesn't respond to browser requests and is stuck with "ImportError: No module named site" warning added to error.log once per second before even the first request.
Found it. It works with Python 2. Looks like mod_wsgi supports Python 3 on paper, but is not implemented, at least not in Ubuntu 13.10 with Apache 2.4.6 and mod_wsgi 3.4.
I didn't manage to make it work with Python3 either. I used the latest version of modWSGI, so it's probably a lack of support for Python3 on its side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.