Python/Django -> Gunicorn error: ModuleNotFoundError: No module named '<project directory>'

I'm trying to deploy a django application with Gunicorn and Nginx on Ubuntu server, but I'm getting this "ModuleNotFoundError". Can anyone help me out with this, please?

The files

gunicorn_myproject.socket

[Unit]
Description=myproject socket

[Socket]
ListenStream=/run/gunicorn_myproject.sock

[Install]
WantedBy=sockets.target

gunicorn_myproject.service

[Unit]
Description=myproject daemon
Requires=gunicorn_myproject.socket
After=network.target

[Service]
User=myuser
Group=www-data
WorkingDirectory=/home/myuser/github/myproject
ExecStart=/home/myuser/github/venvs/myproject/bin/gunicorn \
          --access-logfile - \
          --workers 5 \
          --bind unix:/run/gunicorn_myproject.sock \
          /home/myuser/github/myproject/MyProject.wsgi:application

[Install]
WantedBy=multi-user.target

The error log:

When I run sudo systemctl start gunicorn.socket then sudo systemctl status gunicorn.socket:

● gunicorn_myproject.socket - myproject socket
     Loaded: loaded (/etc/systemd/system/gunicorn_myproject.socket; enabled; vendor preset: enabled)
     Active: active (listening) since Tue 2023-01-31 18:43:11 -03; 1s ago
   Triggers: ● gunicorn_myproject.service
     Listen: /run/gunicorn_myproject.sock (Stream)
     CGroup: /system.slice/gunicorn_myproject.socket

Jan 31 18:43:11 cpro49739 systemd[1]: Listening on myproject socket.

Nothing wrong yet, so then I run curl --unix-socket /run/gunicorn_myproject.sock localhost and get:

curl: (56) Recv failure: Connection reset by peer

Then I run sudo systemctl status gunicorn.service aaaand:

● gunicorn_myproject.service - myproject daemon
     Loaded: loaded (/etc/systemd/system/gunicorn_myproject.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2023-01-31 18:47:29 -03; 1min 33s ago
TriggeredBy: ● gunicorn_myproject.socket
    Process: 2539537 ExecStart=/home/myuser/github/venvs/myproject/bin/gunicorn --access-logfile - --workers 5 --bind unix:/run/gunicorn_myproject.sock /home/myuser/github/myproject/MyProject.wsgi:application (code=exited, status=3)
   Main PID: 2539537 (code=exited, status=3)

Jan 31 18:47:29 cpro49739 gunicorn[2539539]: ModuleNotFoundError: No module named '/home/myuser/github/myproject/MyProject'
Jan 31 18:47:29 cpro49739 gunicorn[2539539]: [2023-01-31 18:47:29 -0300] [2539539] [INFO] Worker exiting (pid: 2539539)
Jan 31 18:47:29 cpro49739 gunicorn[2539537]: [2023-01-31 18:47:29 -0300] [2539537] [WARNING] Worker with pid 2539539 was terminated due to signal 15
Jan 31 18:47:29 cpro49739 gunicorn[2539537]: [2023-01-31 18:47:29 -0300] [2539537] [INFO] Shutting down: Master
Jan 31 18:47:29 cpro49739 gunicorn[2539537]: [2023-01-31 18:47:29 -0300] [2539537] [INFO] Reason: Worker failed to boot.
Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED
Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Failed with result 'exit-code'.
Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Start request repeated too quickly.
Jan 31 18:47:29 cpro49739 systemd[1]: gunicorn_myproject.service: Failed with result 'exit-code'.
Jan 31 18:47:29 cpro49739 systemd[1]: Failed to start myproject daemon.

I tried installing on another folder, with another user, recreate the gunicorn.socket and gunicor.service files, but to no success.

I don't know what more should I do. Any help, please?

Back to Top