Skip to content

Deploying Django and Flask app

Posted on:September 23, 2022 at 03:22 PM

Hosting Flask app on pythonanywhere

  1. Zip the folder using
$ zip -r filename.zip folder
  1. Upload the zipped project folder to pythonanywhere.

  2. Unzip the folder using.

$ unzip filename.zip
  1. Set up virtual env using :
$ mkvirtualenv --python=/usr/bin/python3.6 my-virtualenv

Later you can switch to virtual env using :

$ workon my-virtualenv

Note: location of virtual env = “/home/username/.virtualenvs/myenv”

  1. Go to the Web Tab and hit Add a new web app. Choose Manual Configuration, and then choose the Python version — make sure it’s the same version as the one you used in your virtualenv.

  2. Now go to the Virtualenv section, and enter your virtualenv name: my-virtualenv. When you hit enter, you’ll see it updates to the full path to your virtuaelenv (/home/yourusername/.virtualenvs/my-virtualenv).

  3. In WSGI configuration file (/var/www/saurabhp75_pythonanywhere_com_wsgi.py)

Setting environment variables in pythonanywhere

  1. Save your environment variables into a .env file in your project folder as shown below.
$ cd ~/my-project-dir
$ echo "SECRET_KEY=sekritvalue" >> .env
$ echo "OTHER_SECRET=somethingelse" >> .env
  1. Install python-dotenv into your virtualenv as shown below. Optionally, add it to your requirements.txt, if you’re using one:
$ workon my-virtualenv-name
$ pip install python-dotenv
$ echo python-dotenv >> requirements.txt
  1. For your web app itself (loading your .env file in your WSGI file) Add follwing lines in your WSGI config file.
import os
from dotenv import load_dotenv
project_folder = os.path.expanduser('~/my-project-dir')  # adjust as appropriate
load_dotenv(os.path.join(project_folder, '.env'))
  1. (For bash consoles) If not using virtual env use (a) otherwise (b)

Hosting Django app on pythonanywhere

Following steps are same as in Flask app :

  1. Upload the files to pythonanywhere.
  2. (Try this???) : In setting.py make DEBUG = True.
  3. In settings.py file add following line:
    ALLOWED_HOSTS = [‘saurabhp75.pythonanywhere.com’]
  4. Setting up virtual environment.
  5. Setting up the environment variables.
  6. Editing the WSGI config file (/var/www/yourusername_pythonanywhere_com_wsgi.py)
    Note: sudo pip install python-dotenv
import os
import sys
from dotenv import load_dotenv
project_folder = os.path.expanduser('~/django_blog')  # adjust as appropriate
load_dotenv(os.path.join(project_folder, '.env'))

path = '/home/saurabhp75/django_blog'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'django_blog.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  1. Setting up static files. There are 3 main things to do:

Goto the (Web tab)->(Static Files) on the PythonAnywhere dashboard. Enter the same URL as STATIC_URL in the url section (typically, /static/) Enter the path from STATIC_ROOT into the path section (the full path, including /home/username/etc)