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

TypeError from pkg_resources when using static assets in Pyramid

正在尝试在 Pyramid 应用程序上设置静态资产。我使用了以下调用:

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

文件 main.css 正如预期的那样存储在 toolsofknowledge/static/main.css 下。使用 pip3 install -e ..

可编辑的 link 在本地安装 toolsofknowledge

使用 curl 请求 http://localhost:8080/static/main.css 在服务器上导致此异常:

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

Python3.7,金字塔 1.10.2.

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

问题很简单 -- toolsofknowledge 包不是真正的包,尽管已安装,因为它缺少 __init__.py。这导致 toolsofknowledge.__loader__ 成为 NamespaceLoader 的一个实例,而它应该是 SourceFileLoader。显然,pkg_resources 无法解析与非真实包相关的资源。可以说它应该更顺利地处理这种情况。