Question

I am a very new pyramid learner, I just followed the documentation for creating a new pyramid project : I succeeded to install a virtualenv with pyramid 1.5 packages and run the project in developement mode with 'pserve'.

But, when using apache + mod_wsgi, this is another story : I got this error into the apache error log :

    [Fri May 02 09:08:53 2014] [error] [client 127.0.0.1]   File "/home/eric/perso/projects/Pyratest/pyramid.wsgi", line 8, in <module>
    [Fri May 02 09:08:53 2014] [error] [client 127.0.0.1]     from pyramid.paster import get_app, setup_logging
...
    [Fri May 02 09:08:53 2014] [error] [client 127.0.0.1]   File "/home/eric/perso/projects/venv/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/threadlocal.py", line 3, in <module>
    [Fri May 02 09:08:53 2014] [error] [client 127.0.0.1]     from pyramid.registry import global_registry
    [Fri May 02 09:08:53 2014] [error] [client 127.0.0.1]   File "/home/eric/perso/projects/venv/local/lib/python2.7/site-packages/pyramid-1.5-py2.7.egg/pyramid/registry.py", line 5, in <module>
    [Fri May 02 09:08:53 2014] [error] [client 127.0.0.1]     from zope.interface.registry import Components
    [Fri May 02 09:08:53 2014] [error] [client 127.0.0.1] ImportError: No module named registry

I added a sys.path dump into my pyramid.wsgi :

import sys

print "=" * 80
for p in sys.path:
    print p

from pyramid.paster import get_app, setup_logging

ini_path = '/home/eric/perso/projects/Pyratest/production.ini'
setup_logging(ini_path)
application = get_app(ini_path, 'main')

I can see that the path to zope/interface/registry.py is reachable for the apache user. This path is at the very beginning of the sys.path, so there should not have a path conflict somewhere.

My apache virtualhost definition :

Listen 9999
NameVirtualHost localhost:9999
<VirtualHost localhost:9999>
        ServerAdmin webmaster@localhost

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        ErrorLog /var/log/apache2/pyramid-test-error.log

        LogLevel warn

        CustomLog /var/log/apache2/pyramid-test-access.log combined
        ServerSignature On

        Alias /static/ /home/eric/perso/projects/Pyratest/pyratest/static/
        <Location "/static/">
                <IfModule mod_deflate.c>
                AddOutputFilterByType DEFLATE text/html text/plain text/xml application/javascript
                </IfModule>
                Options Indexes FollowSymLinks MultiViews
                Order allow,deny
                allow from all
        </Location>

        WSGIApplicationGroup %{GLOBAL}
        WSGIPassAuthorization On
        WSGIDaemonProcess pyramid-test user=eric group=eric processes=4 threads=1 python-path=/home/eric/perso/projects/Pyratest/:/home/eric/perso/projects/venv/lib/python2.7/site-packages
        WSGIProcessGroup pyramid-test
        WSGIScriptAlias / /home/eric/perso/projects/Pyratest/pyramid.wsgi

        <Directory /home/eric/perso/projects/venv>
                WSGIProcessGroup eric
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

When I run manually the venv python interpreter, I can import zope.interface.registry without any problem. I do not understand why with mod_wsgi, it does not work. I tried many times, by creating many venv and populating by using easy_install only or pip only, with or without --no-site-packages, I have taken the latest version of virtualenv,distribute,pip, setuptools, but still the same problem. Only one constant : only zope.interface.registry seems to be unreachable : why this one and not other pyramid packages ?

UPDATE 1 : The registry.py and .pyc are readable :

eric@pcubuntu:~/perso/projects$ . ./venv/bin/activate
(venv)eric@pcubuntu:~/perso/projects$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zope.interface.registry
>>> print zope.interface.registry.__file__
/home/eric/perso/projects/venv/lib/python2.7/site-packages/zope.interface-4.1.1-py2.7-linux-x86_64.egg/zope/interface/registry.pyc
>>> 

(venv)eric@pcubuntu:~/perso/projects$ ls /home/eric/perso/projects/venv/lib/python2.7/site-packages/zope.interface-4.1.1-py2.7-linux-x86_64.egg/zope/interface/registry.pyc
/home/eric/perso/projects/venv/lib/python2.7/site-packages/zope.interface-4.1.1-py2.7-linux-x86_64.egg/zope/interface/registry.pyc
(venv)eric@pcubuntu:~/perso/projects$ ls -la /home/eric/perso/projects/venv/lib/python2.7/site-packages/zope.interface-4.1.1-py2.7-linux-x86_64.egg/zope/interface/registry.pyc
-rw-rw-r-- 1 eric eric 25127 mai    2 08:54 /home/eric/perso/projects/venv/lib/python2.7/site-packages/zope.interface-4.1.1-py2.7-linux-x86_64.egg/zope/interface/registry.pyc
(venv)eric@pcubuntu:~/perso/projects$ ls -la /home/eric/perso/projects/venv/lib/python2.7/site-packages/zope.interface-4.1.1-py2.7-linux-x86_64.egg/zope/interface/registry.py
-rw-rw-r-- 1 eric eric 18877 mai    2 08:54 /home/eric/perso/projects/venv/lib/python2.7/site-packages/zope.interface-4.1.1-py2.7-linux-x86_64.egg/zope/interface/registry.py

Update 2: here are the installed packages :

(venv)eric@pcubuntu:~/perso/projects/venv$ pip freeze
Chameleon==2.15
Mako==0.9.1
MarkupSafe==0.21
PasteDeploy==1.5.2
Pygments==1.6
Pyratest==0.0
WebOb==1.3.1
argparse==1.2.1
pyramid==1.5
pyramid-chameleon==0.1
pyramid-debugtoolbar==2.0.2
pyramid-mako==1.0.2
repoze.lru==0.6
translationstring==1.1
venusian==1.0a8
waitress==0.8.8
wsgiref==0.1.2
zope.deprecation==4.1.1
zope.interface==4.1.1

Update 3 : I finally found a way to start pyramid, but it is so weird that I cannot consider that as a solution :

- use pip install instead of easy_install
- add "import pip" at the top of the pyramid.wsgi file    (!!!)

Update 4 : I Redo the exact procedure of the documentation here Running a Pyramid Application under mod_wsgi, that is : a modwsgi directory, a virtualenv within and finally Pyratest within virtualenv : still the same thing : does not work with apache mod_wsgi : ImportError: No module named registry

Update 5 : I decided to install from scratch a brand new ubuntu 12.04 by using vagrant, by this way, everything is working fine. So the question is why it does not work on my usual desktops : for both, it does not work... I run Django on them without any problem, may be there are some tunings that conflict with pyramid...

Was it helpful?

Solution

I finally found a way :

The problem was : mod_wsgi do not provide the same sys.path as python in virtualenv. To fix that I added at the top of the pyramid.wsgi :

import sys
sys.path = [
    '/home/eric/perso/projects/venv/lib/python2.7/lib-dynload',
    '/usr/lib/python2.7',
]


activate_this = '/home/eric/perso/projects/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

OTHER TIPS

In case you have not yet read it, there is an official tutorial for Running a Pyramid Application under mod_wsgi.

What do you get for:

import zope.interface.registry
print(zope.interface.registry.__file__)

when run in command line Python?

In other words, where does it actually exist?

And what version of mod_wsgi are you using?


UPDATE 1

You say based on __file__ it exists under:

/home/eric/perso/projects/venv/lib/python2.7/site-packages

yet the site-packages directory you add to python-path option to the WSGIDaemonProcess directive does not match and instead uses:

/home/eric/perso/projects/venv/local/lib/python2.7/site-packages

Maybe you should make the directory given to python-path match.

After reading these guidelines I would recommend to move your python project Pyratest into your virtualenv.

home/eric/perso/projects/venv/Pyratest/

This could solve several issues regarding permissions. This way mod_wsgi Directory directive applies to your python project AND your virtualenv. This directory layouts complies with referenced pyramid cookbook article and common pyramid scaffold usage. Your current mod_wsgi setup only applies to virtualenv, not to your Pyratest directory.

I further changed WSGIDaemonProcess and WSGIProcessGroup to pyramid to comply with cookbook article. According to your pip freeze your own package Pyratest was installed in virtualenv using python setup.py develop. Thus we only have to pass a single directory to python-path argument. Now this setup looks similar to the one in the cookbook. Make sure apache user and eric can get into these directories as well.

    WSGIApplicationGroup %{GLOBAL}
    WSGIPassAuthorization On
    WSGIDaemonProcess pyramid user=eric group=eric processes=4 threads=1 python-path=/home/eric/perso/projects/venv/lib/python2.7/site-packages
    WSGIScriptAlias / /home/eric/perso/projects/venv/Pyratest/pyramid.wsgi

    <Directory /home/eric/perso/projects/venv>
            WSGIProcessGroup pyramid
            Order allow,deny
            Allow from all
    </Directory>

My recommendation would be to start from scratch by creating a new venv. After you got, repeat it again to be sure your assumptions work. Good luck

For further debugging purposes I recommend mod_wsgi basic docs to build a mod_wsgi setup step-by-step.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top