简体   繁体   中英

TypeError from pkg_resources when using static assets in Pyramid

Trying to set up static assets on a Pyramid app. 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. The toolsofknowledge package is installed locally with an editable link, using pip3 install -e. .

Requesting http://localhost:8080/static/main.css with curl causes this exception on the server:

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.

Solution: add __init__.py to toolsofknowledge directory.

The problem is quite simple -- the toolsofknowledge package was not a real package, despite being installed, as it was missing an __init__.py . This causes toolsofknowledge.__loader__ to be an instance of NamespaceLoader , when it should be SourceFileLoader . Obviously, pkg_resources cannot resolve resources relative to something that's not a real package. Arguably it should handle this case more smoothly though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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