Forums

New to programing with version compatibility question.

TL;DR Wrote webapp using py3.6 and flask 0.12. I don't think any of my code uses anything new python 3.6 or flask 0.12 but would there be differences in background stuff that would affect compatibility with PA?

I've recently picked up learning Python as a hobby I started with codecademy and now I'm going through Head First Python 2nd edition. I'm trying to get the webapp they have you make working here on PythonAnywhere, I'm pretty sure that I've followed their instructions exactly and I'm unable to get it working here. It works fine on my computer using the loopback IP, but I'm having issues here. So I think the issue isn't with my code.

I was wondering even though the uses py 3.4 and flask 0.10 and I copied the code exactly would the fact that I'm running 3.6 and 0.12 respectively effect compatibility with PythonAnywhere on the back end of things? Like the fact that they have you make your own module to use with the webapp so something an issue might come up with using the tar file for that since it was made using 3.6 and as of right now PA only supports up to 3.5?

Hi there, what error are you seeing in your error log?

Yeah probably could have been a bit more specific on what was happening. So let me just go over the steps I took to try and get this going. These are all taken from the Head First Python book Appendix B.

  1. Uploaded a zip file of my webapps folder from my computer and the archive file of my module that the webapp uses; each named webapp.zip and vsearch-1.0.tar.gz respectively.

  2. Opened a bash console to installed my vsearch module as a private module by running the line:

    python3 -m pip install vsearch-1.0.tar.gz --user

Got a success message in the bash console after that

  1. Unpacked my webapp zip file then moved it to mysite folder by running these lines of code:

    unzip qebapp.zip
    mv webapp/* mysite
    
  2. Went to the Web tab of the dashboard and went through the process of adding a new web app selected flask then the most current versions of Python and Flask supported by PA.

  3. Opened the WSGI config file from the web tab of the dashboard and on the last line of the code changed flask_app to vsearch4web.

  4. Then back at the web tab i hit the green Reload button then when i go to frenchy117.pythonanywhere.com i get the PA "Something went wrong" page.

Here is the code for webapp

from flask import Flask, render_template, request, escape
from vsearch import search_for_letters

app = Flask(__name__)


def log_request(req: 'flask_request', res: str) -> None:
    with open('vsearch.log', 'a') as log:
        print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|')


@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = 'Here are your results.'
    results = str(search_for_letters(phrase, letters))
    log_request(request, results)
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_letters=letters,
                           the_results=results,)


@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
    return render_template('entry.html',
                           the_title='Welcome to search_for_letters on the web!')


@app.route('/viewlog')
def view_the_log() -> str:
    contents = []
    with open('vsearch.log') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    return str(contents)

if __name__ == '__main__':                           
    app.run(debug=True)

Look in your error log. That will tell you what's happening when you get the error.

Ahh yes the error log. Seems like it's an issue with my vsearch module not being imported correctly so i'm now reading https://help.pythonanywhere.com/pages/DebuggingImportError/ to see if I can figure out where I went wrong on my own. But here is the log entry into the error log anyways:

2017-02-27 15:33:51,304 :Error running WSGI application
2017-02-27 15:33:51,305 :ImportError: No module named 'vsearch'
2017-02-27 15:33:51,305 :  File "/var/www/frenchy117_pythonanywhere_com_wsgi.py", line 16, in <module>
2017-02-27 15:33:51,305 :    from vsearch4web import app as application
2017-02-27 15:33:51,305 :
2017-02-27 15:33:51,305 :  File "/home/frenchy117/mysite/vsearch4web.py", line 2, in <module>
2017-02-27 15:33:51,306 :    from vsearch import search_for_letters
2017-02-27 15:33:51,306 :***************************************************
2017-02-27 15:33:51,306 :If you're seeing an import error and don't know why,
2017-02-27 15:33:51,306 :we have a dedicated help page to help you debug: 
2017-02-27 15:33:51,306 :https://help.pythonanywhere.com/pages/DebuggingImportError/
2017-02-27 15:33:51,306 :***************************************************
2017-02-27 15:33:51,338 :Error running WSGI application
2017-02-27 15:33:51,339 :ImportError: No module named 'vsearch'
2017-02-27 15:33:51,339 :  File "/var/www/frenchy117_pythonanywhere_com_wsgi.py", line 16, in <module>
2017-02-27 15:33:51,339 :    from vsearch4web import app as application
2017-02-27 15:33:51,339 :
2017-02-27 15:33:51,339 :  File "/home/frenchy117/mysite/vsearch4web.py", line 2, in <module>
2017-02-27 15:33:51,339 :    from vsearch import search_for_letters
2017-02-27 15:33:51,339 :***************************************************
2017-02-27 15:33:51,339 :If you're seeing an import error and don't know why,
2017-02-27 15:33:51,339 :we have a dedicated help page to help you debug: 
2017-02-27 15:33:51,339 :https://help.pythonanywhere.com/pages/DebuggingImportError/
2017-02-27 15:33:51,339 :***************************************************
2017-02-27 15:33:51,506 :Error running WSGI application
2017-02-27 15:33:51,507 :ImportError: No module named 'vsearch'
2017-02-27 15:33:51,507 :  File "/var/www/frenchy117_pythonanywhere_com_wsgi.py", line 16, in <module>
2017-02-27 15:33:51,507 :    from vsearch4web import app as application
2017-02-27 15:33:51,507 :
2017-02-27 15:33:51,507 :  File "/home/frenchy117/mysite/vsearch4web.py", line 2, in <module>
2017-02-27 15:33:51,507 :    from vsearch import search_for_letters
2017-02-27 15:33:51,508 :***************************************************
2017-02-27 15:33:51,508 :If you're seeing an import error and don't know why,
2017-02-27 15:33:51,508 :we have a dedicated help page to help you debug: 
2017-02-27 15:33:51,508 :https://help.pythonanywhere.com/pages/DebuggingImportError/
2017-02-27 15:33:51,508 :***************************************************

In my downtime here at work I've finally been able to work through all the checks on the Debugging Import Error page but I'm not seeing what the issue is.

In my bash console I get no errors when I run:

python3 -i  /var/www/frenchy117_pythonanywhere_com_wsgi.py

I also have no issues when in bash and I run this from the console:

from vsearch import search_for_letters

After I do that I'm even able to call on and run the function search_for_letters without issue:

>>> from vsearch import search_for_letters
>>> search_for_letters('this is a test')
{'i', 'e', 'a'}

I'm even able to print the path to my vsearch module as well:

>>> import vsearch
>>> print (vsearch)
<module 'vsearch' from '/home/frenchy117/.local/lib/python3.4/site-packages/vsearch.py'>

So at this point I'm not sure where the issue is. Is it the path for vsearch? Do the web apps on PA not look in the site-packages folder? I thought they would. I don't believe I need to worry about any init.py since my module is a .py file and not a folder.

I'm guessing that it is something small that I'm missing or overlooked.

Hmm, it might be easiest if I take a look at your code. May I? We can see it from our admin interface, but we always ask for permission first.

Yes you can look. Any help would be greatly appreciated.

Ah, I think I've got it. You did python3 -m pip install vsearch-1.0.tar.gz --user to install your vsearch package. That is aliased to Python 3.4. Your web app is using Python 3.5, which is why it can't see it. When you run python3 from a bash console, you get Python 3.4 again, so you can see it.

Probably the easiest fix is to re-install the vsearch module using Python 3.5, ie.

python3.5 -m pip install vsearch-1.0.tar.gz --user

That was it. Thank you so much. I may not have been able to figure out what was wrong on my own. But with you pointing out the python version differences it made me think about when I looked up the file path for vsearch.py it was in .../lib/python3.4/site-.... and then that would cause issues since i selected 3.5 when setting up the web app here on AP.

Once again thank you for all your help.

Excellent, glad I could help!