Search code examples
pythondjangogunicornegg

Debian 7 + django + gunicorn + virtualenv : site-packages egg-link not in pythonpath


Context :

  • Debian 7
  • gunicorn (installed from debian package in order to use /etc/gunicorn.d configuration)
  • Project dependencies installed via pip in a virtualenv

Dependencies are installed using pip install -r requirements.txt In this file we have 3 packages which comes from github repository.

eg.

-e git+https://github.com/truc/bidule.git#egg=bidule-master

Here my gunicorn.d configuration :

CONFIG = {
    'mode': 'django',
    'environment': {
        'PYTHONPATH': '/path/to/my/django/project/:/path/to/virtualenv/lib/python2.7/site-packages/',
    'PRODUCTION': '1',
     },
    'working_dir': '/path/to/my/django/project/',
    'user': 'user',
    'group': 'group',
    'args': (
        '--bind=127.0.0.1:8090',
        '--workers=3',
        # '--worker-class=egg:gunicorn#sync',
        #'--timeout=30',
    #'--preload',
    'myproject.settings'
    ),
}

But the 3 github packages are not in /path/to/virtualenv/lib/python2.7/site-packages/ so modules are not found in path. I've noticed that there is a file : bidule.egg-link which contain a path to /path/to/virtualenv/src/bidule-master

For the moment the only workaround i found is to add manually path to theses packages in PYTHONPATH in gunicorn.d conf.

'PYTHONPATH': '/path/to/my/django/project/:/path/to/virtualenv/src/bidule-master/:/path/to/virtualenv/lib/python2.7/site-packages/',

My question is there is a way to not have to put each github package manually in pythonpath ?

Thanks


Solution

  • First of all, the django-specific mode of Gunicorn is deprecated, and doesn't work anymore with Django 1.7. See https://github.com/benoitc/gunicorn/issues/705 and https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/gunicorn/

    For your specific problem, instead of tweaking $PYTHONPATH, try setting the 'python' key to the python binary in your virtualenv. It should work out automatically where the modules are located.

    Complete config for Django 1.7:

    CONFIG = {
        # Default mode is WSGI
        'working_dir': '/path/to/my/django/project/',
        'python': '/path/to/virtualenv/bin/python',
        'user': 'user',
        'group': 'group',
        'args': (
            '--bind=127.0.0.1:8090',
            '--workers=3',
            # '--worker-class=egg:gunicorn#sync',
            #'--timeout=30',
            #'--preload',
            'myproject.wsgi'
        ),
    }