Python3 ImportError: cannot import name 'cookies' from 'http'
Python3 ImportError: cannot import name 'cookies' from 'http'
这似乎很常见,可能涉及 Python 2.x 和 Python 3.x 库之间的冲突。例如,这个答案表明问题出在路径上:
但我得到的完整错误是:
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.
这是我根据不同的 Whosebug 答案创建的文件:
/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
问题是你如何调用 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
的开头,导致导入从那里开始。 /usr/local/lib/python3.7/site-packages
不再是根,现在 import http
将导入 /usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py
而不是标准库 http
模块。
调用 gunicorn
的正确方法是调用安装在 bin
目录中的脚本 -- 对于您的示例,它可能位于 /usr/local/bin/gunicorn
.
有关更多信息,我做了一个更彻底的不相关示例 that I usually link to
无关,但你不需要设置PYTHONPATH
,解释器会为你把site-packages
放在sys.path
上
这似乎很常见,可能涉及 Python 2.x 和 Python 3.x 库之间的冲突。例如,这个答案表明问题出在路径上:
但我得到的完整错误是:
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.
这是我根据不同的 Whosebug 答案创建的文件:
/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
问题是你如何调用 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
的开头,导致导入从那里开始。 /usr/local/lib/python3.7/site-packages
不再是根,现在 import http
将导入 /usr/local/lib/python3.7/site-packages/gunicorn/http/__init__.py
而不是标准库 http
模块。
调用 gunicorn
的正确方法是调用安装在 bin
目录中的脚本 -- 对于您的示例,它可能位于 /usr/local/bin/gunicorn
.
有关更多信息,我做了一个更彻底的不相关示例 that I usually link to
无关,但你不需要设置PYTHONPATH
,解释器会为你把site-packages
放在sys.path
上