48

I git cloned a project in my home folder, let's call it /home/telessaude. So the project root is located at /home/telessaude/telessaude_branch_master

If I am inside the Django project home folder ( /home/telessaude/telessaude_branch_master ) and issue a gunicorn comman such as

gunicorn -w 2 -b 0.0.0.0:8000 telessaude.wsgi_dev:application --reload --timeout 900

gunicorn starts and works just fine. However ... if I try to run the same command on one directory above ( /home/telessaude ), I get the following error:

telessaude@ubuntu:~$ gunicorn -w 2 -b 0.0.0.0:8000 telessaude.wsgi_dev:application --reload --timeout 900
[2017-03-22 16:39:28 +0000] [10405] [INFO] Starting gunicorn 19.6.0
[2017-03-22 16:39:28 +0000] [10405] [INFO] Listening at: http://0.0.0.0:8000 (10405)
[2017-03-22 16:39:28 +0000] [10405] [INFO] Using worker: sync
[2017-03-22 16:39:28 +0000] [10410] [INFO] Booting worker with pid: 10410
[2017-03-22 16:39:28 +0000] [10410] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 357, in import_app
    __import__(module)
ImportError: No module named telessaude.wsgi_dev

I also tried running gunicorn in my home folder with

gunicorn -w 2 -b 0.0.0.0:8000 telessaude_branch_master.telessaude.wsgi_dev:application --reload --timeout 900

and

gunicorn -w 2 -b 0.0.0.0:8000 /home/telessaude/telessaude_branch_master/telessaude.wsgi_dev:application --reload --timeout 900

but none of them also worked. Can someone tell me how to fix this? I need to run gunicorn from any folder, because I must add it as a "command" parameter to supervisor.

I'm not using a virtual enviromnment.

2 Answers 2

125

You can use the chdir flag for Gunicorn to change to the project directory before executing your command.

gunicorn -w 2 -b 0.0.0.0:8000 --chdir /home/telessaude/telessaude_branch_master telessaude.wsgi_dev:application --reload --timeout 900
2
  • 1
    Thank you! I was stuck because of not been able to specify the proper command from a systemd service file. The --chdir option made the trick!
    – tebanep
    Jul 28, 2017 at 3:39
  • If you are using a config file, the same is true - i.e. in your dev.py file (in my case), specify a variable called chdir and set it equal to the path you want it to change to.
    – Shmack
    May 31, 2022 at 20:02
1

You should add your django app to the Python Path.

In latest gunicorn, you can try this:

If the root path of django project is /usr/local/src/djangoapp/.

The settings.py path would default to /usr/local/src/djangoapp/djangoapp/settings.py.

gunicorn \
-c /usr/local/src/djangoapp/gunicorn_config.py \
--env DJANGO_SETTINGS_MODULE=djangoapp.settings \
--pythonpath '/usr/local/src/djangoapp' \
djangoapp.wsgi:application
  • -c config file path
  • --env module path of the settings.py in your django project
  • --pythonpath add your project path to Python Path settings.html#pythonpath

--env and --pythonpath are required.

Relative Path also be fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.