How Do You Update Your Web App With Latest Python Modules On Pythonanywhere?
This post is for Python beginners who use Pythonanywhere to host their Python apps for free. Great, you got your web app launched on Pythonanywhere, but there is more to do if you plan to update/upgrade your python web app. The easiest way I know how to do this is to pull the most recent files on Github down to the Pythonanywhere app's root directory. This means you need to initialize your app's root directory with Github before proceeding with this tutorial. You can basically initialize your app's root directory with Github when you code your app with your favorite IDE. I'd done this with Pycharm. When you did this, the first upload of your files to Pythonanywhere should also contain the git files that would allow you to sync with Github later. So, I assume from here on you could do something like git pull from Pythonanywhere's console:
Before, Github allowed you to do the git pull command in Pythonanywhere's console, and it would automatically ask for your GitHub username and password. For a stronger security measure, nowadays, Github requires you to use an SSH key. So, before you could do a git pull, you now need to add a deploy key to Github's repository. To do this, you first need to generate the public and private SSH keys in Pythonanywhere's console. Go to your web app on Pythonanywhere, open up a console, and then enter the command below:
ssh-keygen -t rsa -b 4096 -C "[email protected]_here"
Make sure you replace the "[email protected]_here" part with the email address that you use with GitHub. Once entered the command above, it will prompt you several times, just hit enter to accept the defaults. Done?
Now, you need to go to GitHub, go to your web app repository, go to Settings, and then go to "Deploy keys". Click on "Add deploy key". Go to Pythonanywhere, use the browser, go to Files, go to your user's main folder, go to the .ssh folder, and download the id_rsa.pub file. Basically, the id_rsa.pub file usually locates in /home/web-app-name/.ssh. Open this file and copy the content. Paste this id_rsa.pub content into the text area in the "Add deploy key" on GitHub. Save the key on GitHub
On Pythonanywhere, in the console, go to the app's root directory where .git folder is located, and enter the command below:
git pull [email protected]:YOUR_USERNAME/YOUR_REPO.git
Replace the YOUR_USERNAME with your GitHub username. Replace YOUR_REPO.git with your app's repository name. Once you had done this correctly, in your Pythonanywhere console, you should see all the updated GitHub files get pulled to your Pythonanywhere app's root directory. Let me backtrack just a bit, before doing any GitHub pull, you must make sure that all the files are up to date on GitHub, including the requirements.txt. This way, once you have done the git pull command, you can do a redeploy for your Pythonanywhere web app to update/upgrade your Pythonanywhere web app with the latest modules.
How to do a redeploy for your Pythonanywhere web app? On Pythonanywhere, go to Web, and click on Reload (big green button). Once you have done this, if everything is good, your web app should now be running with the latest Python modules according to your requirements.txt.