Python3 ImportError:无法从“ http”导入名称“ cookies”

杰夫·加兰特

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

导入Python模块失败(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                                    
安东尼·索特蒂尔

问题是您如何调用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

有关其他信息,我制作了一个更彻底的不相关示例,通常将其链接到

无关,但你不应该需要设置PYTHONPATH,解释将会把site-packagessys.path为你

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我该如何解决无法ImportError:无法在python3中导入名称

终端中的python3子进程pip“ImportError:无法导入名称main”

Python请求ImportError:无法导入名称HeaderParsingError

Python ImportError:无法导入名称datafunc [PyML]

Python“ ImportError:无法导入名称”问题

Python,ImportError:无法导入名称AbstractLazySequence

ImportError:无法在Python中导入名称....

Python 3.5“ ImportError:无法导入名称'SomeName'

Python-ImportError:无法导入名称“ HelloW”

Python ImportError:无法导入名称“ __version__”

ImportError:无法导入名称is_python_keyword

python setuptools:ImportError:无法导入名称库

ImportError:无法导入名称

ImportError:无法导入名称

无法使用python3在numpy中导入名称'multiarray'

ImportError:无法从“ urllib3.util.ssl_”导入名称“ ssl”

ImportError:无法从“ bson.py3compat”导入名称“ abc”

pip3 ImportError:无法导入名称“ IncompleteRead”

运行Pip3 ImportError:无法导入名称“ main”

Python Flask蓝图-ImportError:无法导入名称应用

statsmodels ImportError中的python 3.5:无法导入名称“ _representation”

web2py python-ImportError无法导入名称

Django ImportError:无法导入名称“ python_2_unicode_compatible”

PIP(Python):ImportError:无法导入名称_remove_dead_weakref

Python /烧瓶错误:“ ImportError:无法导入名称_compare_digest”

Python PIP引发错误-ImportError:无法导入名称主要

ImportError:无法在python-social-auth中导入名称迁移

Python TwitterAPI问题... ImportError:无法导入名称ReadTimeout

ImportError:无法在python瓶中导入名称路由