0

I'm new to Django. Trying to get my app running under Nginx+gunicorn but running into a few issues. If someone has some insight, I'd appreciate it. I've found a few things to try on google, but gunicorn still seems hosed.

1) I've rsync'd my Django project from my dev box to the Nginx host. If I try launch using the dev server, it doesn't work unless I use Python 3. What have I got messed up?

I've googled the ...from exec error below and found I could get it working if prefixed with python3. Seems like a workaround for a larger issue though since this way of starting it isn't mentioned in any of the tutorials I've been following.

This bombs:

(venv) $ python manage.py runserver 127.0.0.1:8777
  File "manage.py", line 16
    ) from exc
         ^
SyntaxError: invalid syntax

This works:

(venv) $ python3 manage.py runserver 127.0.0.1:8777
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 22, 2019 - 15:46:59
Django version 2.2.7, using settings 'auth.settings'
Starting development server at http://127.0.0.1:8777/
Quit the server with CONTROL-C.
[22/Nov/2019 15:47:27] "GET /accounts/login/ HTTP/1.0" 200 1229
^C(venv) $ 

2) Above, I can run the app fine over Nginx if using the dev server. If executed via gunicorn however, things fall apart. How come? Python says it cannot import name path but it seems to run fine from the dev server (the login page also comes up in the browser correctly) so I'm kinda lost as to what the issue is.

(venv) $ gunicorn --bind 127.0.0.1:8777 auth.wsgi
[2019-11-22 09:43:45 +0000] [2239] [INFO] Starting gunicorn 19.7.1
[2019-11-22 09:43:45 +0000] [2239] [INFO] Listening at: http://127.0.0.1:8777 (2239)
[2019-11-22 09:43:45 +0000] [2239] [INFO] Using worker: sync
[2019-11-22 09:43:45 +0000] [2243] [INFO] Booting worker with pid: 2243
Internal Server Error: /accounts/login/
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 172, in _get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/usr/lib/python2.7/dist-packages/django/urls/resolvers.py", line 364, in resolve
    for pattern in self.url_patterns:
  File "/usr/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/lib/python2.7/dist-packages/django/urls/resolvers.py", line 407, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/lib/python2.7/dist-packages/django/urls/resolvers.py", line 400, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/var/www/django/auth2/auth/urls.py", line 17, in <module>
    from django.urls import path, include
ImportError: cannot import name path
[2019-11-22 09:43:58 +0000] [2239] [INFO] Handling signal: winch
[2019-11-22 09:43:58 +0000] [2239] [INFO] Handling signal: winch
^C[2019-11-22 09:45:06 +0000] [2239] [INFO] Handling signal: int
[2019-11-22 15:45:06 +0000] [2243] [INFO] Worker exiting (pid: 2243)
[2019-11-22 09:45:06 +0000] [2239] [INFO] Shutting down: Master


(venv) $ pip freeze
asn1crypto==0.24.0
backports.os==0.1.1
configparser==4.0.2
contextlib2==0.6.0.post1
cryptography==2.1.4
Django==1.11.26
enum34==1.1.6
future==0.18.2
gunicorn==19.9.0
idna==2.6
importlib-metadata==0.23
ipaddress==1.0.17
keyring==10.6.0
keyrings.alt==3.0
more-itertools==5.0.0
path.py==11.5.2
pathlib2==2.3.5
psycopg2-binary==2.8.4
pycrypto==2.6.1
pygobject==3.26.1
python-apt==1.6.4
pytz==2019.3
pyxdg==0.25
scandir==1.10.0
SecretStorage==2.3.1
six==1.13.0
sqlparse==0.2.4
zipp==0.6.0
(venv) $ 
Server Fault
  • 637
  • 1
  • 6
  • 16
  • I don't understand, when running using `runserver` it says the Django version is 2.2 (which required python 3.5 or higher), but in your virtualenv you have Django 1.11 (and are running on python 2.7). What is you local environment, what does your project actually require? – dirkgroten Nov 22 '19 at 16:21
  • 1
    It looks to me like your project was written for python 3.x and Django 2.2 since you're using the `path` concept. So, on your production machine, your virtualenv is wrong. You need to create a venv with python 3.6 and reinstall all your dependencies, including django 2.2 and gunicorn. – dirkgroten Nov 22 '19 at 16:24
  • Thanks! removing the `venv` directory and recreating it solved the issue. If you want to repost as an answer I can award your internet points ;) – Server Fault Nov 22 '19 at 17:12
  • I'd rather close this question since this is not really helpful to others. It's an issue that just cannot be reproduced anymore. But if you prefer to keep it, I'll add my answer. – dirkgroten Nov 22 '19 at 17:14
  • The `) from exc` error did have some hits on google just not any working answers. I think it would have some benefit for anyone searching on the issue who may have their python environment botched up. Easy to do when your first learning. – Server Fault Nov 22 '19 at 17:31

1 Answers1

0

You need to make sure that the virtualenv you use for launching gunicorn matches the environment you created locally.

As your error trace shows, your venv was created with python 2.7 (and your pip freeze shows it installed Django 1.11).

But your project looks to be setup with Django 2.2 in mind (it uses path) with python 3.x. Which is also what's running when you did runserver.

So you should create a new virtualenv with python3 and reinstall all your dependencies, including Django 2.2 and gunicorn.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42
  • Thanks - removed `venv` and found an issue with `pip` when recreating it. I found a fix for that issue here (https://stackoverflow.com/questions/49836676/error-after-upgrading-pip-cannot-import-name-main). After getting `pip` squared, I recreated the virtualenv directory and reinstalled django, gunicorn, and psycopg2-binary (from here: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04). Gunicorn working now. – Server Fault Nov 22 '19 at 17:54