Pythonanywhere has an excellent tutorial for setting up a website with Flask. The tutorial shows how to set up a website and put it under version control with Git. But it only shows how to do that by initiating a new repository; it doesn’t show you how to connect with an existing repo. In this post, I will show how to connect Pythonanywhere to an existing Github account and some basic Git commands to manage your Pythonanywhere app.
Starting a new project
Go to Github.com and login
Click on “New repository” on the right or open an existing repository if you already have one you want to work with
If it’s new, give it a title and click “Create repository”
Find the URL for the remote repository by clicking on the green “Clone or download” button, then click the “Copy to Clipboard” button to copy the URL. It should end with .git. Something like:
Then go to your bash console on pythonanywhere inside the folder you want to connect to Github. This is possibly your
mysitefolder. You’ll want to initiate this as a git repository. Enter
git remote add origin <remote_repository_URL>. Your remote_repository_URL is the thing we copied above that looks like
Then test out that the connection works. Type:
git remote -v. You should get a response verifying the remote URL. It should look something like this:
origin https://github.com/jss367/pythonanywhere.git (fetch) origin https://github.com/jss367/pythonanywhere.git (push)
Then, you have to pull the remote repository to your local Pythonanywhere folder. Type:
git pull origin master. This is one of the most common Git commands you will use. It is of the format
git pull <remote> <branch>.
Now we’ll want to make a
.gitignorefile. This is a file that contains all the stuff within your local git project that you don’t want to commit with your project. Python cache files are a good example of a file that you don’t want to commit each time. To create a
.gitignorefile, you can create one by typing
touch .gitignore. Files that start with a
.are normally hidden from the user. This is often how people store configuration files that they don’t want to clutter up the users space with.
OK, let’s add some things to it. We’ll do that using vim. Open the file for editing by typing
vim .gitignore. vim is a great tool but it can be very difficult for beginners, so if you’re not familiar with it try to type exactly and only what is written and if you get into a weird state you can always google for more instructions.
First you’ll type
ito go into INSERT mode.
- Once you’re in insert mode, type the following:
To save your work and escape from vim, you’ll need to do a couple things. First, exit INSERT mode by hitting the
Escapekey. Then type in
:wqto write your file and quit the application. Don’t forget the colon at the beginning.
OK, you should be returned to the main console. To check on your work enter
cat .gitignore. Look for the files you added.
- The next thing you’ll need to do is configure git with your information. Enter the following:
git config --global user.email "Your Email" git config --global user.name "Your Name"
Note that your
user.nameis NOT the same as your GitHub user name. My GitHub username is jss367, but my user.name is “Julius Simonelli”.
To double check your changes, you can enter
git config --list. You’ll also see some other configurations that you didn’t add, which is OK.
git statusto see what the status of your repo is. You should see both the
.gitignorefile and the
flask_app.pylisted under “Untracked files”. You’ll need to stage them both for a commit by entering
git add .. By using a
git add .you’ll automatically add every file without typing them individually.
I like to check my
git statusfrequently, just to make sure that I am exactly where I think I am. If you check
git statusagain, it should say “Changes to be committed:” and then list .gitignore and flask_app.py as new files in green.
Then commit your new files with
git commit -m "Add initial flask app and .gitignore files"
- Finally, you’ll push your updated repo to the remote repository. To do this, enter
git push origin master. You may have to enter your Github username and password if you haven’t already. This is your GitHub username, not your git user.name (I know, it’s confusing).
Create a web application
The next thing you’ll need to do is create a web application on PythonAnywhere to connect to your repo. PythonAnywhere makes this easy and it’s address in the tutorial on Python Anywhere’s website, so I won’t go into it further. PythonAnywhere will create a WSGI (pronounced “whiz-gee” kind of like “whiskey”) file for you. Just for reference, here’s an example of what your WSGI file (
jss367_pythonanywhere_com_wsgi.py) should look like:
import sys # ## The "/home/jss367" below specifies your home ## directory -- the rest should be the directory you uploaded your Flask ## code to underneath the home directory. So if you just ran ## "git clone firstname.lastname@example.org/myusername/myproject.git" ## ...or uploaded files to the directory "myproject", then you should ## specify "/home/jss367/myproject" path = '/home/jss367/pythonanywhere' if path not in sys.path: sys.path.append(path) from flask_app import app as application