Fidi RADA
Fidi RADA

Reputation: 21

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

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 ?

Upvotes: 2

Views: 1120

Answers (4)

Swifting
Swifting

Reputation: 479

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

Upvotes: 1

Mattia Astarita
Mattia Astarita

Reputation: 11

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.

Upvotes: 1

Zuby
Zuby

Reputation: 41

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!

Upvotes: 0

pbuck
pbuck

Reputation: 4551

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.

Upvotes: 0

Related Questions