Use this template for continuous deployment of Django apps to PythonAnywhere (a quickstart for Free Accounts using MySQL database).
The repo provides a GitHub workflow that uses PythonAnywhere's API to deploy your code with each new GitHub release.
Below is all you need to know to set up your project in PythonAnywhere and changes you need to make in your GitHub repo. Feel free to contribute with a PR, open a new issue or start a discussion.
Table of contents:
- Guide
- Web App Configuration in PythonAnywhere
- GitHub secrets to add to the repository
- How-to guides and help pages from PythonAnywhere
-
Replace
{{ project_name }}
by Django project name and{{ project_username }}
by your PythonAnywhere username. -
Review and update
settings.py
if needed. This repo uses production settings recommended by Django. You can adjust them based on your needs. -
Generate a new SSH key in a PythonAnyhwere Bash console and add it to your github account
-
Create a new MYSQL database from the
Databases
tab in PA and set MYSQL password. You will be redirected to a page where you can copy database name, host, username and use them insettings.py
(see step 6). -
Create a virtual env in a PythonAnywhere Bash Console in
/home/{{ project_username }}/
directory.
To create a Python 3.10 virtualenv called myvirtualenv
:
mkvirtualenv myvirtualenv --python=/usr/bin/python3.10
- Store your environment variables in a
/home/{{ project_username }}/{{ project_name }}/.env
file.
In /home/{{ project_username }}/{{ project_name }}
run:
echo "export SECRET_KEY=sekritvalue" >> .env
You can use the same file to store other environment variables used in settings.py
:
PA_USERNAME = os.getenv('PROJECT_USERNAME')
DATABASE_NAME = os.getenv('DATABASE_NAME')
DATABASE_USER = os.getenv('DATABASE_USER')
DATABASE_PASSWORD = os.getenv('DATABASE_PASSWORD')
DATABASE_HOST = os.getenv('DATABASE_HOST')
NB: PA_USERNAME
is the same as {{ project_username }}
- Load up the environnment variables from
.env
file:
In /home/{{ project_username }}/{{ project_name }}
run:
set -a; source .env; set +a
You can also add the source
command to your virtualenv postactivate script:
echo 'set -a; source .env; set +a' >> ~/.virtualenvs/myvirtualenv/bin/postactivate
- Clone the GitHub repo from a PythonAnyhwere Bash console in
/home/{{ project_username }}/
directory.
Go to the Web tab in PythonAnywhere and make sure the fields are set correctly:
Source code:
/home/{{ project_username }}/{{ project_name }}
Working directory:
/home/{{ project_username }}/
WSGI configuration file:
/var/www/{{ project_username }}_pythonanywhere_com_wsgi.py
Virtualenv:
/home/myusername/.virtualenvs/{{ virtualenv_name }}
Static files:
URL | Directory |
---|---|
/static/ |
/home/{{ project_username }}/{{ project_name }}/static/ |
WSGI configuration file contents:
# This file contains the WSGI configuration required to serve up your
# web application at http://{{ project_username }}.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Django project
import os
import sys
# add your project directory to the sys.path
project_home = '/home/{{ project_username }}/{{ project_name }}'
if project_home not in sys.path:
sys.path.insert(0, project_home)
# set environment variable to tell django where your settings.py is
os.environ['DJANGO_SETTINGS_MODULE'] = '{{ project_name }}.settings'
# set environment variables (from `.env`)
from dotenv import load_dotenv
project_folder = os.path.expanduser(project_home) # adjust as appropriate
load_dotenv(os.path.join(project_folder, '.env'))
# serve django via WSGI
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
-
${{ secrets.PA_USER }}
: same as{{ project_username }}
-
${{ secrets.PA_VIRTUALENV }}
: same as{{ virtualenv_name }}
created in step 4 -
${{ secrets.PA_TOKEN }}
: The PythonAnywhere API uses token-based authentication. You can get your token from your Account page on the API Token tab -
${{ secrets.PA_CONSOLE_ID }}
: The ID of the console instance in PythonAnywhere used to run the commands (pull changes, migrate, collectstatic, reload app) -
${{ secrets.PA_SRC_DIR }}
: The path to the git repository in PythonAnywhere with the source code (/home/{{ project_username }}/{{ project_name }}
for example) -
${{ secrets.PA_SSH_KEY_PASSPHRASE }}
: The passphrase of the SSH key created in PythonAnywhere
For more details about the PythonAnywhere API: https://help.pythonanywhere.com/pages/API/
The pages below helped create this guide: