Upload existing project to python anywhere

Introduction

In this article I want to share how to upload an existing python project to pythonanywhere.

For those who don’t know, pythonanywhere is a cloud service that you can use to run, edit, host your python project. You may want to deploy your project on pythonanywhere or have an online copy of your project in order to work with it from anywhere, without having to have installed everything on that computer.

In this article I will assume that you already have your own python project on your computer and you are using git for version control tasks, this means that you also have a virtual environment already set locally on your computer.

Let’s start

Create an account

First thing you have to do is install an account on pythonanywhere. You can pay month packages or work for free, chose the plan that fits your needs.

Upload the project and create the virtual environment

Go to the console tab and create a new console. Next you have to clone your code from git

git clone https://github.com/myusername/myproject.git

Next you have to create a virtual environment with the appropriate python version and name

mkvirtualenv --python=/usr/bin/python3.4 mysite-virtualenv

You can manually set the environment or you can take a copy the requirements of that project in a .txt file and then install everything. Use the command line and in the path where your local virtual environment is located run the following commands

source my_env_virtual/bin/activate
pip freeze > requirements.txt

Now you have a requirements.txt file with everything that you need in order to setup your new virtual environment.

Upload that file from the files tab. In the command line that you had previously opened use the following command

pip install -r requirements.txt

By now you must have uploaded your project and set a new virtual environment.

Go online

Few steps left until your project is online!

Go to the Web tab and add a new web app, then chose the Manual Configuration and the version of python you want.

In the Code section of the same tab add to the Source Code field the path that contains the manage.py file

Next in the Virtualenv section of the same tab add the path to your virtual environment.

In the Static Files section of the same tab enter the to the url field the same URL as STATIC_URL in the url section (e.g. /static/) and in the Directory field enter the the path from STATIC_ROOT into the path section (e.g. /home/…/static/)

Finally find the WSGI configuration file section and click on the .py file to edit it.

Remove everything and write the following

# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys

# assuming your django settings file is at '/home/myusername/mysite/mysite/settings.py'
path = '/home/myusername/mysite' # path to settings.py
if path not in sys.path:
sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' # mysite is the project name

## Uncomment the lines below depending on your Django version
###### then, for django >=1.5:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
###### or, for older django <=1.4
#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()

Test it!

Refresh the project by clicking the reload button of the web tab and you can visit your project visiting the mysername.pythonanywhere.com