【问题标题】:Python3 ImportError: cannot import name 'cookies' from 'http'Python3 ImportError:无法从“http”导入名称“cookies”
【发布时间】:2018-08-19 20:44:42
【问题描述】:

这似乎很常见,可能涉及 Python 2.x 和 Python 3.x 库之间的冲突。例如,这个答案表明问题出在路径上:

Import Python module fails (http.cookies)

但我得到的完整错误是:

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

如果我这样做:

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

我明白了:

# -*- 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']

很明显,对于我拥有的HTTP 版本,没有导出cookies 模块。

如果我这样做:

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

我看到两个条目:

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

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

这个文件:

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

开头:

from http import cookies

当它位于http 时,它可以从http 自身导入是否有意义?而http 不会在其__init__.py 文件中导出它?

更新:

如果我运行这个:

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

这是我看到的完整堆栈跟踪:

[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.                                                            

这是我根据不同的 Stackoverflow 答案创建的文件:

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

这个文件的内容是:

#!/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())    

如果我这样做:

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

我明白了:

/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

如果我打印出 sys.path:

for p in sys.path:
    print(p)

我明白了:

/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                                    

【问题讨论】:

  • 您正在运行的脚本和/或PYTHONPATH 的值是什么。 http.cookies是标准库包
  • echo $PYTHONPATH 显示 /usr/local/lib64/python3.7/site-packages/
  • 我会指出,这个问题很常见。当我搜索谷歌时,我会看到很多条目。然而,对其他人有用的东西似乎不适合我的情况。
  • @DannyMoshe -- 我已经读过了,但谢谢。我不认为路径是问题吗?这对您来说似乎是一条不正确的道路吗? ImportError:无法从“http”(/usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py)导入名称“cookies”

标签: django python-3.x python-import


【解决方案1】:

问题是你如何调用guincorn

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

这会将/usr/local/lib/python3.7/site-packages/gunicorn/ 放在sys.path 的开头,导致导入从那里开始。现在import http 将导入/usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py 而不是标准库http 模块,而不是/usr/local/lib/python3.7/site-packages

调用gunicorn 的正确方法是调用安装在bin 目录中的脚本 -- 例如,这可能位于/usr/local/bin/gunicorn

关于更多信息,我做了一个更彻底的无关示例that I usually link to

不相关,但你不应该设置PYTHONPATH,解释器会为你把site-packages放在sys.path

【讨论】:

  • 我一开始只使用“gunicorn”作为命令,我得到了同样的错误,所以我尝试了“python3 gunicorn”,但 Python 抱怨它找不到名为 gunicorn 的文件,所以他们我放入完整路径,然后我不需要“python3”。但这些变化都不起作用。
  • @JeffGallant 我刚刚看到您的编辑——如果您将“某些 SO 帖子”告诉您制作的文件移动到 /usr/local/bin 并运行它,那么它会起作用。该文件应该已经存在了
猜你喜欢
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2021-06-30
  • 2020-10-19
  • 2018-08-13
  • 2015-01-26
  • 2020-10-10
相关资源
最近更新 更多