skip to Main Content

I am running a server with Nginx, flask and Gunicorn on Ubuntu 23.10. The Python modules are installed inside a venv. I set this up by following the instructions here: dev.to . It actually did work for a while and then somehow I messed it up. Now I can’t even get Gunicorn to work and I’m thinking about wiping the server and restarting. This is my systemd file:

Description=description
After=network.target

[Service]
User=<my user name>
Group=www-data
WorkingDirectory=/var/www/<website name>.org/<project name>/<project name>/
Environment="PATH=/home/<user name>/.local/share/virtualenvs/<site name>-rbL82YaN/bin/"
ExecStart=/home/<user name>/.local/share/virtualenvs/<site name>-rbL82YaN/bin/gunicorn --workers 3 --bind unix:/var/www/<site name>/<project name>/<project name>/<project name>.sock wsgi:app

[Install]
WantedBy=multi-user.target

These are the packages in the /home/<user name>/.local/share/virtualenvs/<site name>-rbL82YaN/bin/ directory:

activate      activate.fish  activate.ps1      flask     normalizer  pip3      pip3.11  python3     wheel   wheel-3.11
activate.csh  activate.nu    activate_this.py  gunicorn  pip         pip-3.11  python   python3.11  wheel3  wheel3.11

And this is the error message Gunicorn is giving me:

[2023-11-22 04:17:49 +0000] [95711] [INFO] Starting gunicorn 20.1.0
[2023-11-22 04:17:49 +0000] [95711] [INFO] Listening at: http://127.0.0.1:8000 (95711)
[2023-11-22 04:17:49 +0000] [95711] [INFO] Using worker: sync
[2023-11-22 04:17:49 +0000] [95712] [INFO] Booting worker with pid: 95712
[2023-11-22 04:17:49 +0000] [95712] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()
  File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
                ^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
                    ^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 384, in import_app
    mod = importlib.import_module(module)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/www/zf47r85u8jkg.org/<project name>/<project name>/__init__.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'
[2023-11-22 04:17:49 +0000] [95712] [INFO] Worker exiting (pid: 95712)
[2023-11-22 04:17:49 +0000] [95711] [INFO] Shutting down: Master
[2023-11-22 04:17:49 +0000] [95711] [INFO] Reason: Worker failed to boot.

It doesn’t seem like Gunicorn is starting from the location specified in my systemd file as it says in the error message

File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()

but I am very new to this so that could be completely wrong.

2

Answers


  1. Instead of wiping the server try removing the venv and try setup other environments like pyenv or virtualvenv and reinstall your libraries inside. In case it also don’t work try installing conda environment, conda will work in most cases.

    Login or Signup to reply.
  2. I was facing the same issue recently. Unfortunately I am not sure how the environments were set up (was not done by me), and I don’t have exact details, but I remember it was basically the exact same as the problem asked here.

    There were 2 different virtual envs created, let’s say venv and venv2.
    gunicorn was installed in venv2, and it failed to start due to ModuleNotFoundError, even though I knew that the package causing the error was installed in venv2. Then I saw in the logs that gunicorn was using python from venv, not from venv2.

    The problem was that the gunicorn script at /path/to/.virtualenvs/venv2/bin/gunicorn had the wrong interpreter specified in the file, for example:

    #!/path/to/.virtualenvs/venv/bin/python
    # -*- coding: utf-8 -*-
    import re
    import sys
    from gunicorn.app.wsgiapp import run
    if __name__ == '__main__':
        sys.argv[0] = re.sub(r'(-script.pyw|.exe)?$', '', sys.argv[0])
        sys.exit(run())
    

    So I had to replace

    #!/path/to/.virtualenvs/venv/bin/python
    

    to

    #!/path/to/.virtualenvs/venv2/bin/python
    

    Why gunicorn in venv2 had python interpreter specified from venv in this particular case still remains a mystery to this day.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search