"ImportError: No module named trac.web.main" on fresh install with WSGI

977 views
Skip to first unread message

ams

unread,
May 30, 2017, 2:03:25 PM5/30/17
to Trac Users
Recently rebuilt a server with RHEL7.   I had to reinstall trac too, but after installing I am seeing the error "ImportError: No module named trac.web.main" in the Apache HTTPD log files.

Here are my install steps: 

-----------------------------------------

## Install trac 
pip install trac

## create the wsgi script
trac-admin /path/to/trac/ initenv
trac-admin /path/to/trac/ deploy /tmp/deploy

## examine script
cat /tmp/deploy/cgi-bin/trac.wsgi

[root@nbs]# cat /path/to/trac/cgi-bin/trac.wsgi 
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#
# Copyright (C)2008-2009 Edgewall Software
# Copyright (C) 2008 Noah Kantrowitz <no...@coderanger.net>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Noah Kantrowitz <no...@coderanger.net>
import os

def application(environ, start_request):
   if not 'trac.env_parent_dir' in environ:
       environ.setdefault('trac.env_path', '/path/to/trac')
   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)


## Move trac scripts to correct location
mv /tmp/deploy/* /path/to/trac/

## Set permissions
chown -R apache:apache /path/to/trac/
chmod 700 /path/to/trac/cgi-bin/trac.wsgi

## create trac.conf for Apache HTTPD
WSGIScriptAlias /trac /path/to/trac/cgi-bin/trac.wsgi
<Directory /path/to/trac/>
   WSGIApplicationGroup %{GLOBAL}
   # For Apache 2.2
   <IfModule !mod_authz_core.c>
       Order deny,allow
       Allow from all
   </IfModule>
   # For Apache 2.4
   <IfModule mod_authz_core.c>
       Require all granted
   </IfModule>
</Directory>

<Location "/trac/login">
 AuthType Basic
 AuthName "Trac"
 AuthUserFile /data/www/htpasswd/trac.htpasswd
 Require valid-user
</Location>

## Restart apache
service httpd restart

## browser shows: 
Internal Server Error

## httpd error log shows: 
[DATE] [:error] [pid 3286] [client CLIENT_IP:55491] mod_wsgi (pid=3286): Exception occurred processing WSGI script '/path/to/trac/cgi-bin/trac.wsgi'.
[DATE] [:error] [pid 3286] [client CLIENT_IP:55491] Traceback (most recent call last):
[DATE] [:error] [pid 3286] [client CLIENT_IP:55491]   File "/path/to/trac/cgi-bin/trac.wsgi", line 30, in application
[DATE] [:error] [pid 3286] [client CLIENT_IP:55491]     from trac.web.main import dispatch_request
[DATE] [:error] [pid 3286] [client CLIENT_IP:55491] ImportError: No module named trac.web.main


## If I point trac.conf WSGI to a test script: 
def application(environ, start_response):
    start_response('200 OK',[('Content-type','text/html')])
    return ['<html><body>Hello World!</body></html>']

I see the "Hello World" in the browser.  WSGI appears to be working OK.

Something is off in the trac installation but I am unsure what exactly.   Help is greatly appreciated. 

ams

Logan Anderson

unread,
May 30, 2017, 2:34:39 PM5/30/17
to trac-...@googlegroups.com
You have the following:

       environ.setdefault('trac.env_path', '/path/to/trac')

Does your wsgi file literally say '/path/to/trac' ? Or have you properly configured it for your actual path?



From: "ams" <adrya.st...@gmail.com>
To: "Trac Users" <trac-...@googlegroups.com>
Sent: Tuesday, May 30, 2017 2:03:25 PM
Subject: [Trac] "ImportError: No module named trac.web.main" on fresh install with WSGI

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+...@googlegroups.com.
To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

Adrya Stembridge

unread,
May 30, 2017, 2:39:23 PM5/30/17
to trac-...@googlegroups.com
It is properly configured with the actual path.  I did a find/replace of actual my path before posting.   Never can be too careful.  

To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Trac Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/trac-users/yeheTRZRjys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to trac-users+unsubscribe@googlegroups.com.

Logan Anderson

unread,
May 30, 2017, 2:46:50 PM5/30/17
to trac-...@googlegroups.com
Are you using mod_wsgi compiled for the wrong version of python or something of that nature? mod_wsgi has to be compiled for python 2.7. You are referencing #!/usr/bin/python2 but if the other modules are not compiled for the same version then you will run into errors such as this. Also, not certain if this is an issue for you but generally I use something more like #!/usr/bin/env python-2.7 for my shebang.


This is what my demo trac.wsgi looks like. I am using virtualenv:

import sys
sys.stdout = sys.stderr



import os
activate_this = os.path.expanduser("/var/www/html/trac/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))


os.environ['TRAC_ENV_DIR'] = '/var/www/html/trac'
os.environ['PYTHON_EGG_CACHE'] = '/var/www/html/trac/logan/eggs'


import trac.web.main
def application(environ, start_response):
  environ['trac.env_path'] = '/var/www/html/trac/logan'
  return trac.web.main.dispatch_request(environ, start_response)






From: "Adrya Stembridge" <adrya.st...@gmail.com>
To: trac-...@googlegroups.com
Sent: Tuesday, May 30, 2017 2:39:14 PM
Subject: Re: [Trac] "ImportError: No module named trac.web.main" on fresh install with WSGI

To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+...@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Trac Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/trac-users/yeheTRZRjys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to trac-users+...@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+...@googlegroups.com.

Dimitri Maziuk

unread,
May 30, 2017, 3:02:41 PM5/30/17
to trac-...@googlegroups.com
On 05/30/2017 01:45 PM, Logan Anderson wrote:
> Are you using mod_wsgi compiled for the wrong version of python or something of that nature? mod_wsgi has to be compiled for python 2.7. You are referencing #!/usr/bin/python2 but if the other modules are not compiled for the same version then you will run into errors such as this. Also, not certain if this is an issue for you but generally I use something more like #!/usr/bin/env python-2.7 for my shebang.

On centos 7 /usr/bin/python2 is a symlink to python 2.7. I'd pprint
sys.path on line 29 and see what python path actually is in the wsgi
playpen.

--
Dimitri Maziuk
Programmer/sysadmin
BioMagResBank, UW-Madison -- http://www.bmrb.wisc.edu

signature.asc

Adrya Stembridge

unread,
May 30, 2017, 3:03:36 PM5/30/17
to trac-...@googlegroups.com
mod_wsgi tests ok with the sample "Hello World" script included in the original post.    I see "Hello World" in the browser with no errors in httpd.    I'm not well versed in Python, but if it works OK with a simple test script wouldn't that indicate it is installed and functioning properly (as a baseline)? 

To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Trac Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/trac-users/yeheTRZRjys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to trac-users+unsubscribe@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Trac Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/trac-users/yeheTRZRjys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to trac-users+unsubscribe@googlegroups.com.

Logan Anderson

unread,
May 30, 2017, 3:17:54 PM5/30/17
to trac-...@googlegroups.com
Not if that test script is simple enough that it conforms to both 2.7 and 3.x standards and will execute in either scenario.

You should be able to check the log file to see which version is being called. Like I said, are there any other log entries which may be relevant?


From: "Adrya Stembridge" <adrya.st...@gmail.com>
To: trac-...@googlegroups.com
Sent: Tuesday, May 30, 2017 3:03:32 PM
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+...@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Trac Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/trac-users/yeheTRZRjys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to trac-users+...@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+...@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Trac Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/trac-users/yeheTRZRjys/unsubscribe.
To unsubscribe from this group and all its topics, send an email to trac-users+...@googlegroups.com.

To post to this group, send email to trac-...@googlegroups.com.
Visit this group at https://groups.google.com/group/trac-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+...@googlegroups.com.

RjOllos

unread,
Jun 4, 2017, 6:35:41 PM6/4/17
to Trac Users
The problem seems to be that trac is not found on your python path.

To start, I would open a shell and see if "import trac" succeeds. 

Also, the path in your directory directive seems to be incorrect, and should be:
<Directory /path/to/trac/cgi-bin>

- Ryan

Steve Weiss

unread,
Sep 18, 2017, 4:49:16 PM9/18/17
to Trac Users
I am getting this same error. I'm new to trac and python, the "hello world" test script works fine. When I type "python" and get a prompt, I typed "import trac" and was returned to a prompt. There were no errors, I assume this means it worked? I've been searching around and several people with this problem found that it was a permission issue, but I have no idea on which files or directories. The apache user owns the directory where the trac projects are located (/usr/local/data/trac).

The error in the apache log is:

Enter code here...Sep 18 16:36:07 neon httpd: [Mon Sep 18 16:36:07.686471 2017] [:error] [pid 17696] [client 156.68.64.176:54205] mod_wsgi (pid=17696): Exception occurred processing WSGI script '/usr/local/data/trac/projects/share/static/cgi-bin/trac.wsgi'., referer: http://localhost/
Sep 18 16:36:07 neon httpd: [Mon Sep 18 16:36:07.686533 2017] [:error] [pid 17696] [client 156.68.64.176:54205] Traceback (most recent call last):, referer: http://localhost/
Sep 18 16:36:07 neon httpd: [Mon Sep 18 16:36:07.686557 2017] [:error] [pid 17696] [client 156.68.64.176:54205]   File "/usr/local/data/trac/projects/share/static/cgi-bin/trac.wsgi", line 30, in application, referer: http://localhost/
Sep 18 16:36:07 neon httpd: [Mon Sep 18 16:36:07.686650 2017] [:error] [pid 17696] [client 156.68.64.176:54205]     from trac.web.main import dispatch_request, referer: http://localhost/
Sep 18 16:36:07 neon httpd: [Mon Sep 18 16:36:07.686680 2017] [:error] [pid 17696] [client 156.68.64.176:54205] ImportError: No module named trac.web.main, referer: http://localhost/




RjOllos

unread,
Sep 21, 2017, 7:51:22 PM9/21/17
to Trac Users


On Monday, September 18, 2017 at 1:49:16 PM UTC-7, Steve Weiss wrote:
I am getting this same error. I'm new to trac and python, the "hello world" test script works fine. When I type "python" and get a prompt, I typed "import trac" and was returned to a prompt. There were no errors, I assume this means it worked? I've been searching around and several people with this problem found that it was a permission issue, but I have no idea on which files or directories. The apache user owns the directory where the trac projects are located (/usr/local/data/trac).

Try:
$ sudo -u www-data python
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import trac

If that fails, you may need to set python-home in WSGIDaemonProcess, or using WSGIPythonHome:

- Ryan
Reply all
Reply to author
Forward
0 new messages