简体   繁体   中英

I keep running into issues with web.py

Okay, so I actually had an issue with this line of code beforehand but I realized, thanks to a fellow answer from this site, that I had to delete my web.py folder from my directory and reinstall an updated version. I decided to go with the developer version.

Upon doing so I noticed that my initial run of code worked, but after I updated it and refreshed my browser, I got a huge error. I have tried to fix this several times, and I reinstalled like 10 times. I went into debugger.py and found no real solution, as every time I updated it the code refused to run.

To better illustrate my error this is my code:

import web

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

app = web.application(urls, globals())
class index:
    def GET(self, name):
        print ("Hello", name, '. How are you today?')
if __name__=="__main__":
    app.run()

It runs without an error, but when open my page or refresh it after an initial run, it gives me this:

在此处输入图片说明

Error:

<class 'AttributeError'> at /
'NoneType' object has no attribute 'name'

Now keep in mind that my first run works, and if I restart my computer or reinstall web.py , it works the first time every time. But after that initial run, it just gives me the above error on the webpage.

This happens only when you have made some changes to your script. You can stop script ( ctrl+c ) and start it again, everything should work fine.

But i found workaround. On the top of your script (after imports) write:

__name__ = '<your main file name>'  # main2 for main2.py in my case

on the bottom replace

if __name__ == "__main__":

with

if __spec__ is None:

So the full script (main2.py) should look like:

import web
__name__ = 'main2'


urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello:
    def GET(self):
        return 'Hello, world1!'

if __spec__ is None:
    app.run()

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