Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 ImportError: cannot import name 'cookies' from 'http'

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                                    
like image 960
JeffGallant Avatar asked Jul 15 '26 09:07

JeffGallant


1 Answers

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

like image 56
Anthony Sottile Avatar answered Jul 17 '26 22:07

Anthony Sottile