Matt Lovett
Matt Lovett

Reputation: 11

Flask app fails to run when deployed to pythonanywhere

I'm working on a web app that makes calls to the movie database. Everything runs fine when it's running on localhost. I'm attempting to deploy it on pythonanywhere. I have created a virtual environment and installed all the dependencies. I believe I have edited the WSGI file correctly.

import sys
path = '/home/gcmatt/capstone/src'
if path not in sys.path
   sys.path.append(path)
from app import app as application

At least from the error messages I get it appears to have found the files. When I try to load the page it fails to run. It appears that for some reason the code won't import my own functions src.models.search.

2018-03-31 15:28:50,341: Error running WSGI application
2018-03-31 15:28:50,349: ModuleNotFoundError: No module named 'src'
2018-03-31 15:28:50,349:   File 
"/var/www/gcmatt_pythonanywhere_com_wsgi.py", line 82, in <module>
2018-03-31 15:28:50,349:     from app import app as application  # noqa
2018-03-31 15:28:50,349: 
2018-03-31 15:28:50,349:   File "/home/gcmatt/capstone/src/app.py", 
line 4, in <module>
2018-03-31 15:28:50,350:     from src.models.search import Search 

Do I need to alter the file structure or file paths in the python code for the app to run? I followed the tutorials on python anywhere and feel that I have set everything up correctly.

This is what my directory structure looks like

capstone/
|--.git/
|--src/
   |models/
    |--__init__.py
    |--results.py
    |--search.py
  |--templates
    |--base.html
    |--home.html
    |--noresults.html
    |--search.html
  |--__init__.py
  |--api_file.txt
  |--app.py
  |--requirements.txt

Upvotes: 1

Views: 561

Answers (1)

Gabriel Boorse
Gabriel Boorse

Reputation: 71

Since you set your path = '/home/gcmatt/capstone/src', the src module is not available on path. Only models, templates etc are.

Python is only going to recognize a package if it can see a subdirectory with an __init__.py in it. It will not recognize the current directory as a package, regardless of whether the current directory has __init__.py or not.

Try setting path = '/home/gcmatt/capstone' and it should be able to detect the package src.

Edit here: you may also want to say from src.app import app as application

Second edit here: In your app.py you should also change your imports to import subpackages instead of src.[something]. For instance: from models.search import Search. Your WSGI will see the package src, and import src.app from it. And then src.app imports packages relative to itself (i.e. import models).

Source: I use PythonAnywhere for a lot of my apps :)

Here is some further reading on regular Python packages.

Upvotes: 5

Related Questions