WEBPY / Serving static files : 'StaticApp Object has no attribute 'directory'

微笑、不失礼 提交于 2020-01-04 05:16:08

问题


i've got some issues serving files on my web.py application : Python 3.7 web.py ver0.40_Dev1 Eclipse Photon Release (4.8.0)

app.py :

import web

render = web.template.render('templates/')

urls = (
    '/', 'index',)

class index:
    def GET(self):
        return render.index()

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

templates/index.html :

<html>

    <h2>HELLO</h2>          <img src = "/static/image.png">

</html>

I manually created /templates and /static folders And I get the following error when displaying my index on localhost :

> AttributeError("'StaticApp' object has no attribute 'directory'")
> Traceback (most recent call last):   File
> "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web/wsgiserver/wsgiserver3.py",
> line 1089, in communicate
>     req.respond()   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web/wsgiserver/wsgiserver3.py",
> line 877, in respond
>     self.server.gateway(self).respond()   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web/wsgiserver/wsgiserver3.py",
> line 1982, in respond
>     for chunk in response:   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web/httpserver.py",
> line 255, in __iter__
>     path = self.translate_path(self.path)   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/server.py",
> line 820, in translate_path
>     path = self.directory AttributeError: 'StaticApp' object has no attribute 'directory'

Trying : (with image at the root)

<img src = "/image.png">

returns casual 404 error (files are supposed to be served from /static right?)

And reaching : localhost/static

returns "not found"

http://webpy.org/cookbook/staticfiles seems to tell me files serving from /static file is automatically handled by web.py ? (or i don't get it)

Anyway : i can't serve any file in my app.py...

any help ?


回答1:


Webpy is not updated for python 3.7. Installing python 3.6.1 solved the problem for me. It should work for any python 3.6 sub-versions.




回答2:


There is a pull request waiting a while now to be merged into web.py: https://github.com/webpy/webpy/pull/468/files

Adjust a file from webpy inside your site-packages: web/httpserver.py

Add:

self.directory = os.getcwd()

After:

self.start_response = start_response

It's now working with me running Python 3.7.3 with web.py 0.40.dev1




回答3:


Your code is good, but it appears you have a mixup with your python libraries.

'directory' was introduced into http.server.SimpleHTTPRequestHandler in python 3.7. This is the class webpy's StaticApp inherits from. However, it appears your compiled version of web.py is using an older version of SimpleHTTPRequestHandler, which does not define directory. I don't know how this can happen, though you mention you're using Eclipse, which (I'm guessing) is handling the compilation dependencies.

I suggest you clear out the compiled .pyc / .pyo and try again.




回答4:


I've just started learning Python in the last 2 days, so forgive any n00b issues with this.

Having struggled with the exact same issue for an hour or so, and baffled at why there seems to be no resolution yet...I kind of hacked about and seemed to get this working until someone more official provides a real fix.

Two things to do:

  1. I made a change to server.py in ‎⁨/⁨Library/⁨Frameworks/Python.framework/⁨Versions/⁨3.7⁩/lib/⁨python3.7/⁨http⁩ and amended line 820 as follows:

    path = self.directory was amended to path = self.path

    While this seemed to get rid of the StaticApp object bla bla error, I was still left with continual 404's when trying to load static content. This led me to find a small post on the internet which led to the following...

  2. Instead of running my Python app by hitting Run inside PyCharm, I ran python3 controller.py in a Terminal window, where controller.py is my Python MVC controller - and low and behold this seemed to work!

I have no honest idea why running in Terminal instead of PyCharm worked - but thought I'd drop it here.

Hopefully someone brings a fix soon, this is quite a ballache when trying to learn a new language!



来源:https://stackoverflow.com/questions/52439325/webpy-serving-static-files-staticapp-object-has-no-attribute-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!