Python app import error in Django with WSGI gunicorn

心已入冬 提交于 2019-12-01 06:25:01

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?

Well above method has been deprecated by gunicorn now. When I tried the same if failed with warning message!

(venv)root@ip-172-31-23-172:~/myproj# python manage.py run_gunicorn 0.0.0.0:8001

!!!

!!! WARNING: This command is deprecated.

!!!

!!! You should now run your application with the WSGI interface

!!! installed with your project. Ex.:

!!!

!!! gunicorn myproject.wsgi:application

!!!

!!! See https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/gunicorn/

!!! for more info.

!!!

Hence, I tried below command and it worked for me.

gunicorn myproject.wsgi:application

Add "gunicorn" into settings.py/INSTALLED_APPS and use

python manage.py run_gunicorn 127.0.0.0:8001

(whatever the port you like) For more info visit : https://pypi.python.org/pypi/gunicorn/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!