Deploying DRF or Django App on pythonanywhere.com less than 10 minutes.
In this blog, I would like to share how to deploy your django-app including DRF(Django-Restful-Framework) app into pythonanywhere.com. Using pythonanywhere.com, you can deploy any django app less than 10 miniute.
- First you go to www.pythonanywhere.com.
- Click “Web” category on the top-right side and click “add a new app”. This will give you a pop-up like the picture below. Unless you have bought a domain already, you should choose your-id.pythonanywhere.com.
*For your information, I had another app “www.kivyschool.com”.
3. Next decide your app name and directory. I recommend you to leave the directory path as it is unless you know what you are doing. My project name is DRF_BACKEND and I set the path on the very top of my home directory.
4. Choose Django and python version. I choose python3.5 due to easy virtualenv setup at pythonanywhere.com. I recommend you to do so as well.
5. Go to “your-id.pythonanywhere.com”. You will see a page like the following picture. Django has been deployed.
6. Your django app has been deployed. However, the default version of django at pythonanywere.com is 1.10 or 1.11. I want the lastest django version which is 2.1.7 as of this writing. In order to use the newest django, you have to create a virtualenv and configure it on pythonanywere.com. That means, you should create a virtualenv on console by yourself, and tell pythonanywhere.com where to look up when you run the application.
7. Go to “Consoles” and create a virtualenv
For a virtualenv, I usually make “venvs” folder and create individual virtualenvs inside the venvs folder in case I need a few more virtualenvs. So in my case I made “venvs” folders like /home/tofusoup429/venvs/, then I placed the virtualenv inside the folder. It is totally up to you where to place your virtualenv and what name to use.
The command line for placing vitualenv is as follows.
00:56 ~/venvs $ virtualenv -p python3.5 DRF_BACKEND_venv
OK. you will see somthing like this if it works as it is supposed.
Using base prefix '/usr'New python executable in /home/tofusoup429/venvs/DRF_BACKEND_venv/bin/python3.5Not overwriting existing python script /home/tofusoup429/venvs/DRF_BACKEND_venv/bin/python (you must use /home/tofusoup429/venvs/DRF_BACKEND_venv/bin/python3.5)Installing setuptools, pip, wheel...done.
8. Now activate the virtualenv. Make sure you type in path correctly.
00:58 ~/venvs $ source DRF_BACKEND_venv/source/activate
9. you will see your virtualenv name within parentheses as follows.
(DRF_BACKEND_venv) 00:59 ~/venvs $
10. Ok… it is time to install the latest django.
(DRF_BACKEND_venv) 00:59 ~/venvs $ pip install django
It is supposed to show this.
Looking in links: /usr/share/pip-wheelsCollecting django Downloading https://files.pythonhosted.org/packages/c7/87/fbd666c4f87591ae25b7bb374298e8629816e87193c4099d3608ef11fab9/Django-2.1.7-py3-none-any.whl (7.3MB) 100% |████████████████████████████████| 7.3MB 569kB/s Collecting pytz (from django) Using cached https://files.pythonhosted.org/packages/61/28/1d3920e4d1d50b19bc5d24398a7cd85cc7b9a75a490570d5a30c57622d34/pytz-2018.9-py2.py3-none-any.whlInstalling collected packages: pytz, django
Do you see the django version 2.1.7? It is sucessfully installed.
11. Configure virtualenv at pythonanywhere.com
Now we should tell pythonanywere which virtualenv I am going to use by configuring. Go to “Web” and Virtualenv section. Do you see red letters saying “Enter path to a virtual env if desired”? Yes, we desire to set our own virtualenv. So type the path of it. In my case, it is /home/tofusoup429/venvs/DRF_BACKEND_venv
If you type in correctly, you will see something like this.
Make sure your python version in virtualenv matches with the one above. If it does not match, a warning appears.
12. Reload your site by clicking the green button including your url.
13. Go to “your-id.pythonanywhere.com”, in my case tofusoup429.pythonanywhere.com, and see what is different.
It is the new version of django. The latest django is deployed. It probably took you less than 10 minutes. You can put stuffs you have worked on here.
14. You can replace the ugly your-id.pythonanywere.com with nice your custom-domain like www.kivyschool.com like I do. Pythonanywhere requires you to have a paid-account, which is starting from $5 a month to do so. I will share how to replace it in a later blog post.
Thank you for reading. I was in a rush when writing this. So if you find any error, please let me know. Also if you have any comments, questions or whatever, leave a message, I will get back to you as soon as possible.