这似乎很常见,可能涉及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
答案 0 :(得分:2)
问题在于您如何调用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
上