2

This seems to be a very common, possibly involving a conflict between Python 2.x and Python 3.x libraries. For instance, this answer suggests the problem is with the path:

Import Python module fails (http.cookies)

but the full error that I get is:

ImportError: cannot import name 'cookies' from 'http' (/usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py)

and if I do this:

cat /usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py

I see:

# -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.

from gunicorn.http.message import Message, Request
from gunicorn.http.parser import RequestParser

__all__ = ['Message', 'Request', 'RequestParser']

So clearly, for the version of HTTP that I have, there is no cookies module exported.

If I do this:

find /usr/local/lib64/python3.7/site-packages/   -name  cookie.py

I see two entries:

/usr/local/lib64/python3.7/site-packages/django/contrib/messages/storage/cookie.py

/usr/local/lib64/python3.7/site-packages/django/http/cookie.py

This file:

/usr/local/lib64/python3.7/site-packages/django/http/cookie.py

starts with:

from http import cookies

Does it make sense that this can import from itself from http, when it lives in http? And http does not export this in its __init__.py file?

UPDATE:

If I run this:

/usr/local/lib/python3.7/site-packages/gunicorn/gunicorn    ecommerce.wsgi:application --bind 0.0.0.0:8000

This is the full stacktrace that I see:

[2018-08-19 21:19:15 +0000] [14987] [INFO] Starting gunicorn 19.9.0
[2018-08-19 21:19:15 +0000] [14987] [INFO] Listening at: http://0.0.0.0:8000 (14987)
[2018-08-19 21:19:15 +0000] [14987] [INFO] Using worker: sync
[2018-08-19 21:19:15 +0000] [14990] [INFO] Booting worker with pid: 14990
[2018-08-19 21:19:15 +0000] [14990] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/usr/share/lynette-ecomerce-demo/ecommerce-site/version1/django/ecommerce/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
  File "/usr/local/lib64/python3.7/site-packages/django/core/wsgi.py", line 2, in <module>
    from django.core.handlers.wsgi import WSGIHandler
  File "/usr/local/lib64/python3.7/site-packages/django/core/handlers/wsgi.py", line 8, in <module>
    from django.core.handlers import base
  File "/usr/local/lib64/python3.7/site-packages/django/core/handlers/base.py", line 7, in <module>
    from django.urls import get_resolver, set_urlconf
  File "/usr/local/lib64/python3.7/site-packages/django/urls/__init__.py", line 1, in <module>
    from .base import (
  File "/usr/local/lib64/python3.7/site-packages/django/urls/base.py", line 8, in <module>
    from .exceptions import NoReverseMatch, Resolver404
  File "/usr/local/lib64/python3.7/site-packages/django/urls/exceptions.py", line 1, in <module>
    from django.http import Http404
  File "/usr/local/lib64/python3.7/site-packages/django/http/__init__.py", line 1, in <module>
    from django.http.cookie import SimpleCookie, parse_cookie
  File "/usr/local/lib64/python3.7/site-packages/django/http/cookie.py", line 1, in <module>
    from http import cookies
ImportError: cannot import name 'cookies' from 'http' (/usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py)
[2018-08-19 21:19:15 +0000] [14990] [INFO] Worker exiting (pid: 14990)
[2018-08-19 21:19:15 +0000] [14987] [INFO] Shutting down: Master
[2018-08-19 21:19:15 +0000] [14987] [INFO] Reason: Worker failed to boot.                                                            

This is a file that I created based on a different Stackoverflow answer:

/usr/local/lib/python3.7/site-packages/gunicorn/gunicorn 

The contents of this file are:

#!/usr/bin/python3

#-*- 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())    

If I do this:

find /usr/local/lib/python3.7/   -name cookies.py

I see:

/usr/local/lib/python3.7/site-packages/cookies.py
/usr/local/lib/python3.7/site-packages/future/backports/http/cookies.py
/usr/local/lib/python3.7/site-packages/future/moves/http/cookies.py
/usr/local/lib/python3.7/site-packages/requests/cookies.py

If I print out sys.path:

for p in sys.path:
    print(p)

I get:

/usr/local/lib/python3.7/site-packages/gunicorn
/usr/local/lib64/python3.7/site-packages
/usr/lib64/python37.zip
/usr/lib64/python3.7
/usr/lib64/python3.7/lib-dynload
/usr/local/lib/python3.7/site-packages
/usr/lib64/python3.7/site-packages
/usr/lib/python3.7/site-packages
/usr/local/lib/python3.7/site-packages/gunicorn/..
/usr/local/lib/python3.7/site-packages/gunicorn/../project                                    
JeffGallant
  • 409
  • 2
  • 6
  • 17
  • What is the script that you are running and/or the value of `PYTHONPATH`. `http.cookies` is a standard library package – anthony sottile Aug 19 '18 at 20:49
  • echo $PYTHONPATH shows /usr/local/lib64/python3.7/site-packages/ – JeffGallant Aug 19 '18 at 21:03
  • Does this help - https://stackoverflow.com/questions/20078774/import-python-module-fails-http-cookies?rq=1 – DannyMoshe Aug 19 '18 at 21:04
  • I'll point out, the question is very common. When I search Google I see a lot of entries. Yet the things that worked for others don't seem to be appropriate for my case. – JeffGallant Aug 19 '18 at 21:05
  • @DannyMoshe -- I had already read that, but thank you. I don't think the path is the problem? Does this seem like an incorrect path to you? ImportError: cannot import name 'cookies' from 'http' (/usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py) – JeffGallant Aug 19 '18 at 21:07
  • @JeffGallant can you paste the entire stacktrace including the command that you ran into the original post? – anthony sottile Aug 19 '18 at 21:11
  • I think so..I believe it should be coming from Python3.7/Lib/http/cookies.py. – DannyMoshe Aug 19 '18 at 21:18
  • I updated the above post, to include the full stacktrace. – JeffGallant Aug 19 '18 at 21:20

1 Answers1

2

The problem is how you're invoking guincorn:

/usr/local/lib/python3.7/site-packages/gunicorn/gunicorn    ecommerce.wsgi:application --bind 0.0.0.0:8000

This puts /usr/local/lib/python3.7/site-packages/gunicorn/ onto the beginning of sys.path causing imports to be rooted from there. Instead of /usr/local/lib/python3.7/site-packages being the root, now import http will import /usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py instead of the standard library http module.

The correct way to invoke gunicorn is to invoke the script installed into the bin directory -- For your example, this is probably located at /usr/local/bin/gunicorn.

For additional information, I've made a more thorough unrelated example that I usually link to

unrelated, but you shouldn't need to set PYTHONPATH, the interpreter will put site-packages on sys.path for you

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • I started off with just "gunicorn" as the command and I got the same error, so then I tried "python3 gunicorn" but Python complained that it could not find a file called gunicorn, so they I put in the full path, and then I didn't need the "python3". But none of these variations worked. – JeffGallant Aug 20 '18 at 01:31
  • @JeffGallant I just saw your edit -- if you move that file that "some SO post" told you to make into `/usr/local/bin` and run it then it'll work. that file should exist already there though – anthony sottile Aug 20 '18 at 01:33