Forums

Sorry about this but i'm confused

Hey sorry about this but i'm terribly confused, i've been trying to get pythonanywhere working with web.py for a while and i understand that it comes with a web.py starter in wsgi.py but i have no idea how to link that wsgi.py with files from my dropbox folder or any other folder for that matter. Maybe i'm just being a massive noob but i really don't understand how this system works.

Hi bambo,

No need to be sorry! Maybe this will make it clearer. The example for web.py that we include in the wsgi file is below.

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

class index:
    def GET(self):
        web.header('Content-Type','text/html; charset=utf-8', unique=True)
        return """
            <html>
            <head>
                <title>Python Anywhere hosted web application</title>
            </head>
            <body>
            <h1>Hello, World!</h1>
            <p>
                This is the default welcome page for a
                <a href="http://www.pythonanywhere.com/">PythonAnywhere</a>
                hosted web application.
            </p>
            <p>
                Find out more about how to configure your own web application
                by visiting the <a href="http://www.pythonanywhere.com/web_app_setup/">web app setup</a> page
            </p>
            </body>
            </html>"""

# comment out these two lines if you want to use another framework
app = web.application(urls, globals())
application = app.wsgifunc()

You could move all of that code into a file at "/home/bambo/my_webapp.py".

Then inside the wsgi.py file you would just place these lines of code:

import os
import sys

path = "/home/bambo/"
if path not in sys.path:
    sys.path.append(path)

from my_webapp import application

(EDIT - My bad, I had a trailing .py on the end of my_webapp, cut and paste error)

Does that make it clearer? The wsgi.py file just needs to contain an object called "application", that object needs to be a valid WSGI application. Where you import "application" doesn't matter. But you do need to be able to import it which is why we need to add the directory you are importing it from to your path.

Ahh! that make everything so much clearer!

Thanks a lot hansel.

well i did exactly as you said, and i get a 500 internal server error. Very strange

[Tue May 01 15:49:04 2012] Traceback (most recent call last): [Tue May 01 15:49:05 2012] File "/bin/serve_wsgi.py", line 53, in main [Tue May 01 15:49:05 2012] application = get_application_with_error_logger(error_log) [Tue May 01 15:49:05 2012] File "/bin/serve_wsgi.py", line 46, in get_application_with_error_logger [Tue May 01 15:49:05 2012] wsgi_module = imp.load_source('wsgi', '/var/www/wsgi.py') [Tue May 01 15:49:05 2012] File "/var/www/wsgi.py", line 8, in <module> [Tue May 01 15:49:05 2012] from code import application [Tue May 01 15:49:05 2012] ImportError: cannot import name application

i tried with "from code.py import application" but that gave a different error about not finding the module.

Importing things always trips people up. My general piece of advice is to add some debugging code. You can use the call os.getcwd() and print sys.path to find out where your program is running and what the current path is set to.

In your specific instance I would first check the module "code" doesn't contain any syntax errors by running "python code.py". Then I would check the code itself. The path is correct because the logs are telling you that. Maybe you haven't actually defined "application" inside the code.py file?

try another file name rather than code.py and it may work

As mopodo points out, your choice of filename is conflicting with Python's builtin code module. So, it finds that module earlier in the include path, but then fails because (not surprisingly) it doesn't define the name application to be imported.

I would strongly recommend renaming the file to something you can be sure is unique, such as bamboapp.py - this should fix your issues.

If you're really attached to the name code.py, however, you can probably make it work by subtly changing your wsgi.py file to this:

import os
import sys

path = "/home/bambo/"
if not sys.path or sys.path[0] != path:
    sys.path.insert(0, path)

from code import application

I should point out that with this approach any .py files you have in this directory will hide Python builtins or system libraries, which could create serious headaches for you. You have control over which libraries you import, but you may break libraries that they import which is a lot harder to figure out.

In short, just rename to something unique - it'll make your life a lot easier.

Thank you!

It was very useful for beginners

:-)

@faustinoaq: Welcome to PA! I hope you love it here!!