WSGI with Python 2.7 virtualenv problem

170 views
Skip to first unread message

ERIK MEITNER

unread,
Mar 22, 2022, 3:23:23 PM3/22/22
to modwsgi
Hello,
I am attempting to set up Trac using a virtualenv under mod_wsgi on Apache 2.4.41.

I've run into a problem where I cannot get the Python script to run under the Python2.7 interpreter from the virtualenv.

I am able to run Trac independent of Apache/WSGI so I think the problem lies in the WSGI configuration.


The paths referrred to below are:
/var/www/trac1.4 - Trac project environment
/var/www/trac1.4-venv - Python2.7 virtualenv with "trac" module installed via pip


Apache config:
------
WSGIPythonHome /var/www/trac1.4-venv

<VirtualHost *:80>

    ServerName trac.mydomain
    ServerAdmin webmaster@mydomain
    DocumentRoot /var/www/trac1.4/htdocs/
    WSGIDaemonProcess trac python-home=/var/www/trac1.4-venv
    WSGIScriptAlias / /var/www/trac1.4/site/cgi-bin/trac.wsgi
    WSGIProcessGroup trac
    WSGIApplicationGroup %{GLOBAL}
</VirtualHost>
-------


The trac.wsgi script:
---------------
#!/var/www/trac1.4-venv/bin/python2.7
# -*- coding: utf-8 -*-
import os

import sys

# for testing:
print('sys.prefix       = %s' % repr(sys.prefix))
print('sys.path         = %s' % repr(sys.path))
print('sys.executable   = %s' % (sys.executable))
print('sys.version_info = %s.%s.%s' % (sys.version_info[0:3]))


def application(environ, start_request):
    if not 'trac.env_parent_dir' in environ:
        environ.setdefault('trac.env_path', '/var/www/trac1.4')
    if 'PYTHON_EGG_CACHE' in environ:
        os.environ['PYTHON_EGG_CACHE'] = environ['PYTHON_EGG_CACHE']
    elif 'trac.env_path' in environ:
        os.environ['PYTHON_EGG_CACHE'] = \
            os.path.join(environ['trac.env_path'], '.egg-cache')
    elif 'trac.env_parent_dir' in environ:
        os.environ['PYTHON_EGG_CACHE'] = \
            os.path.join(environ['trac.env_parent_dir'], '.egg-cache')
    from trac.web.main import dispatch_request
    return dispatch_request(environ, start_request)
---------------



Note what is logged when the script runs. Python V3.8.10 is the interpreter being used.
-----------------
[Tue Mar 22 12:32:40.155975 2022] [wsgi:error] [pid 1756932] sys.prefix       = '/var/www/trac1.4-venv'
[Tue Mar 22 12:32:40.156044 2022] [wsgi:error] [pid 1756932] sys.path         = ['/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload']
[Tue Mar 22 12:32:40.156072 2022] [wsgi:error] [pid 1756932] sys.executable   = /var/www/trac1.4-venv/bin/python
[Tue Mar 22 12:32:40.156100 2022] [wsgi:error] [pid 1756932] sys.version_info = 3.8.10


[Tue Mar 22 12:32:40.156527 2022] [wsgi:error] [pid 1756932] [remote 10.130.176.220:48756] mod_wsgi (pid=1756932): Exception occurred processing WSGI script '/var/www/trac1.4/site/cgi-bin/trac.wsgi'.
[Tue Mar 22 12:32:40.165606 2022] [wsgi:error] [pid 1756932] [remote 10.130.176.220:48756] Traceback (most recent call last):
[Tue Mar 22 12:32:40.165667 2022] [wsgi:error] [pid 1756932] [remote 10.130.176.220:48756]   File "/var/www/trac1.4/site/cgi-bin/trac.wsgi", line 30, in application
[Tue Mar 22 12:32:40.165682 2022] [wsgi:error] [pid 1756932] [remote 10.130.176.220:48756]     from trac.web.main import dispatch_request
[Tue Mar 22 12:32:40.165756 2022] [wsgi:error] [pid 1756932] [remote 10.130.176.220:48756] ModuleNotFoundError: No module named 'trac'
-----------------


The virtualenv IS python2.7:
--------
me@rudin:~$ . /var/www/trac1.4-venv/bin/activate
(trac1.4-venv) me@rudin:~$ python -V
Python 2.7.18
--------

I'd really appreciate it if you could show me what I'm doing wrong.


Graham Dumpleton

unread,
Mar 22, 2022, 3:26:18 PM3/22/22
to mod...@googlegroups.com
The installed mod_wsgi is compiled for Python 3.8. You would need to uninstall that mod_wsgi version (likely the OS system package) and install mod_wsgi compiled for Python 2.7.

It is not possible to force mod_wsgi compiled for one Python version to use a virtual environment for a different Python version. This is explained in:

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/8eb50796-0abb-4c92-9fd4-e8fa91d19ae6n%40googlegroups.com.

ERIK MEITNER

unread,
Mar 22, 2022, 3:34:09 PM3/22/22
to modwsgi
Agh. You know I read that but I thought the mod_wsgi I was using was for 2.7. Now that I look I see it's for 3.8. Sorry about the noise.
Reply all
Reply to author
Forward
0 new messages