简体   繁体   English

在 Pyramid 中使用 static 资产时来自 pkg_resources 的 TypeError

[英]TypeError from pkg_resources when using static assets in Pyramid

Trying to set up static assets on a Pyramid app.尝试在 Pyramid 应用程序上设置 static 资产。 I used the following call:我使用了以下调用:

config.add_static_view(name='static', path='toolsofknowledge:static')

The file main.css is stored under toolsofknowledge/static/main.css , as expected.正如预期的那样,文件main.css存储在toolsofknowledge/static/main.css下。 The toolsofknowledge package is installed locally with an editable link, using pip3 install -e. toolsofknowledge使用pip3 install -e. . .

Requesting http://localhost:8080/static/main.css with curl causes this exception on the server:使用 curl 请求http://localhost:8080/static/main.css 8CBA22E28EB17B5F5C6AE2A266AZ 会在服务器上导致此异常:

amoe@cslp019129 $ PYRAMID_RELOAD_TEMPLATES=1 pserve3 --reload development.ini
Starting monitor for PID 3796.
Starting server in PID 3796.
Serving on http://localhost:8080
Serving on http://localhost:8080
ERROR:waitress:Exception when serving /static/main.css
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pyramid/tweens.py", line 13, in _error_handler
    response = request.invoke_exception_view(exc_info)
  File "/usr/lib/python3/dist-packages/pyramid/view.py", line 769, in invoke_exception_view
    raise HTTPNotFound
pyramid.httpexceptions.HTTPNotFound: The resource could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/waitress/channel.py", line 336, in service
    task.service()
  File "/usr/lib/python3/dist-packages/waitress/task.py", line 175, in service
    self.execute()
  File "/usr/lib/python3/dist-packages/waitress/task.py", line 452, in execute
    app_iter = self.channel.server.application(env, start_response)
  File "/usr/lib/python3/dist-packages/pyramid/router.py", line 270, in __call__
    response = self.execution_policy(environ, self)
  File "/usr/lib/python3/dist-packages/pyramid/router.py", line 279, in default_execution_policy
    return request.invoke_exception_view(reraise=True)
  File "/usr/lib/python3/dist-packages/pyramid/view.py", line 768, in invoke_exception_view
    reraise_(*exc_info)
  File "/usr/lib/python3/dist-packages/pyramid/compat.py", line 179, in reraise
    raise value
  File "/usr/lib/python3/dist-packages/pyramid/router.py", line 277, in default_execution_policy
    return router.invoke_request(request)
  File "/usr/lib/python3/dist-packages/pyramid/router.py", line 249, in invoke_request
    response = handle_request(request)
  File "/usr/lib/python3/dist-packages/pyramid/tweens.py", line 43, in excview_tween
    response = _error_handler(request, exc)
  File "/usr/lib/python3/dist-packages/pyramid/tweens.py", line 17, in _error_handler
    reraise(*exc_info)
  File "/usr/lib/python3/dist-packages/pyramid/compat.py", line 179, in reraise
    raise value
  File "/usr/lib/python3/dist-packages/pyramid/tweens.py", line 41, in excview_tween
    response = handler(request)
  File "/usr/lib/python3/dist-packages/pyramid/router.py", line 148, in handle_request
    registry, request, context, context_iface, view_name
  File "/usr/lib/python3/dist-packages/pyramid/view.py", line 657, in _call_view
    response = view_callable(context, request)
  File "/usr/lib/python3/dist-packages/pyramid/viewderivers.py", line 401, in viewresult_to_response
    result = view(context, request)
  File "/usr/lib/python3/dist-packages/pyramid/static.py", line 102, in __call__
    if resource_isdir(self.package_name, resource_path):
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1138, in resource_isdir
    return get_provider(package_or_requirement).resource_isdir(
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 364, in get_provider
    return _find_adapter(_provider_factories, loader)(module)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1392, in __init__
    self.module_path = os.path.dirname(getattr(module, '__file__', ''))
  File "/usr/lib/python3.7/posixpath.py", line 156, in dirname
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

Python 3.7, Pyramid 1.10.2. Python 3.7,金字塔 1.10.2。

Solution: add __init__.py to toolsofknowledge directory.解决方法:将__init__.py添加到toolsofknowledge目录。

The problem is quite simple -- the toolsofknowledge package was not a real package, despite being installed, as it was missing an __init__.py .问题很简单toolsofknowledge package 不是真正的 package,尽管已安装,因为它缺少__init__.py This causes toolsofknowledge.__loader__ to be an instance of NamespaceLoader , when it should be SourceFileLoader .这会导致toolsofknowledge.__loader__成为NamespaceLoader的一个实例,而它应该是SourceFileLoader Obviously, pkg_resources cannot resolve resources relative to something that's not a real package.显然, pkg_resources无法解析与不是真正的 package 的东西相关的资源。 Arguably it should handle this case more smoothly though.可以说它应该更顺利地处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM