Python > Deployment and Distribution > Deployment Platforms > Platform as a Service (PaaS) (e.g., Heroku, PythonAnywhere)
Deploying a Simple Web App to PythonAnywhere
This snippet demonstrates deploying a basic Flask application to PythonAnywhere. It covers creating a web app, configuring the virtual environment, and setting up the WSGI configuration file.
Prerequisites
Before you begin, make sure you have the following: * **PythonAnywhere Account:** You'll need a PythonAnywhere account. You can sign up for free at [https://www.pythonanywhere.com/](https://www.pythonanywhere.com/). * **Basic understanding of WSGI:** Web Server Gateway Interface, is a simple calling convention for web servers to forward requests to web applications or frameworks written in Python.
Create a Basic Flask Application
This code creates a simple Flask application with a single route that returns 'Hello from PythonAnywhere!'. Save this as `flask_app.py` (or any name you prefer, but remember it for later steps).
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello from PythonAnywhere!'
if __name__ == '__main__':
app.run(debug=True)
Create a Virtual Environment
Log in to PythonAnywhere and open a Bash console. Create a virtual environment for your project. Replace `myvirtualenv` with your preferred name: bash mkvirtualenv myvirtualenv --python=python3.10 # Or your preferred Python version workon myvirtualenv
Install Dependencies
Install Flask (and any other dependencies) within your virtual environment.
pip install Flask
Move Your Application Files
Upload your `flask_app.py` (and any other related files) to your PythonAnywhere account. You can use the file upload feature in the PythonAnywhere web interface or use `scp` from your local machine.
Create a Web App
Go to the 'Web' tab on PythonAnywhere and click 'Add a new web app'. Choose 'Manual configuration' and select the Python version that matches your virtual environment.
Configure the WSGI File
PythonAnywhere creates a WSGI configuration file for you. Find this file (it will be named something like `/var/www/
Reload Your Web App
Click the green 'Reload' button on the 'Web' tab to apply your changes.
Real-Life Use Case
PythonAnywhere is well-suited for hosting small to medium-sized web applications, personal projects, and learning environments. It's especially useful for beginners due to its simplified interface and free tier.
Best Practices
Pros
Cons
FAQ
-
What is a WSGI file?
A WSGI (Web Server Gateway Interface) file is a Python script that acts as an interface between your web application and the web server. It specifies how the server should handle requests and pass them to your application. -
Why am I getting a 'ModuleNotFoundError'?
This usually indicates that your virtual environment is not properly activated, or that your WSGI file is not correctly pointing to your application's directory. Double-check the paths in your WSGI file and ensure your virtual environment is active. -
How do I update my application?
To update your application, make changes to your code, upload the updated files to your PythonAnywhere account, and then reload your web app from the 'Web' tab.