Python app import error in Django with WSGI gunicorn

后端 未结 3 1470
感动是毒
感动是毒 2021-01-13 07:34

I\'m trying to deploy a Django app with gunicorn on Heroku and I\'ve run into a few hitches.

When I began my project my Django version was 1.3 and didn\'t contain th

3条回答
  •  迷失自我
    2021-01-13 08:10

    Figured out my problem. Needed to add the project directory to Python path, not the app directory - i.e., topturk/top instead of topturk/top/turk in order to import turk directory modules.

    python top/manage.py run_gunicorn
    

    and

    python top/manage.py runserver
    

    were working just fine because as per Python path documentation the directory of the calling module is always added as element 0 in the Python path tuple - and so when top/manage.py was being used, topturk/top was always in the Python path.

    With heroku however, the Procfile is in the parent directory of the project, topturk and not topturk/top, so when the Procfile commands are run topturk is added to the Python path but not topturk/top, and hence the errors.

    In hindsight, figured out this is what the Django documentation was referring to in the last sentence of this section: https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/gunicorn/#running-django-in-gunicorn-as-a-generic-wsgi-application, where they say in order to run this command the project must be on the Python path.

    Problem solved by adding

    sys.path.insert(1, os.path.dirname(os.path.realpath(__file__)))
    

    to either settings.py or wsgi.py - added to settings.py as that seems like what some other people have recommended (http://codespatter.com/2009/04/10/how-to-add-locations-to-python-path-for-reusable-django-apps/), but not sure what the best place to put the insert is. Anyone know?

提交回复
热议问题