Using Python on PythonAnywhere

PythonAnywhere (www.pythonanywhere.com) is a free online service that gives you a way to develop and run Python programs inside a browser. This is a full-featured Linux environment with a browser-based text editor with syntax highlighting. To use Pythonanywhere for this class - all you need to write and run Python code is a web browser. There is nothing to install at all.

This means that you can do this course on a "locked-down" environment on systems like Apple's iPad, iPhone, Android, ChromeBooks, or Windows 10 Home S. You can also use PythonAnywhere if you are using a work or school computer that does not allow any software into be installed.

Sign up for an account

You will need to sign up for an account to use PythonAnywhere. They have a free level that will cover all of your needs for this course through Chapter 15.

PythonAnywhere is committed to letting you have a free account forever as long as you keep logging in and extending it. They have low cost paid plans if you want more disk space or compute power for your own projects or more flexibility or features. But rest assured that their free plan is sufficient for this course.

Writing Your First Program on PythonAnywhere

Once you can log in to PythonAnywhere, go into the files tab and create a new file called hello.py in your home folder (should be something like /home/drchuck). Put the following line in the file:

print('Hello world')
Save the file and press Run and you should see:
Hello world
>>>
Then change the text to 'Hello PY4E world', press Save and press Run and it should run your modified program.

While the Run button works for programs that are a few lines, once you start working on more complex programs you will need to use a Linux shell (command line). It might feel a little strange at the beginning but learning a little bit of Linux is a great idea as it is the dominant system that is used for servers.

Using the Linux Shell on PythonAnywhere

This works best if you can have two tabs open at the same time in the browser. One tab should be navigated to the Files screen and another should be nagivated to the Consoles screen. If you already have a bash console running you can go back to it - otherwise start a new Bash console. After it starts up, you should see something like:

14:12 ~ $
This is the Linux command prompt. Lets run your 'hello.py' program from the command line:
14:12 ~ $ cd
14:14 ~ $ pwd
/home/drchuck
14:15 ~ $ ls -l
-rw-rw-r--  1 drchuck registered_users   27 Mar 29 14:15 hello.py
14:16 ~ $ python3 hello.py
Hello PY4E world
14:16 ~ $
Here is what the commands are doing: We recommend that you start using the Linux bash shell to run your code from the very beginning because eventually you will need to use bash to run more complex programs.

Some Cool Hints on the bash console

You can scroll back through previous commands by pressing the up and down arrows and re-execute commands using the enter key. This can save a lot of typing. If you like keeping your screen uncluttered, you can clear the scroll back buffer by pressing the Command key and the K key at the same time.

Editing Files on PythonAnywhere

There are three ways to edit files in your PythonAnywhere environment, ranging from the easiest to the coolest. You only have to edit the file one of these ways.

  1. Go to the main PythonAnywhere dashboard, browse files, navigate to the correct folder and edit the file.
  2. Or in the command line:

    cd ~
    nano hello.py
    

    Save the File by pressing CTRL-X, Y, and Enter.

  3. Don't try this most difficult and most cool way to edit files on Linux without a helper if it is your first time with the vi text editor.
    cd ~
    vi hello.py
    
    Once you have opened vi, cursor down where you want to make a change, and press the i key to go into 'INSERT' mode, then type your new text and press the esc key when you are done. To save the file, you type :wq followed by enter. If you get lost press esc :q! enter to get out of the file without saving.

If you aleady know some _other_ command line text editor in Linux, you can use it to edit files. In general, you will find that it often quicker and easier to make small edits to files in the command line rather than a full screen UI. And once you start deploying real applications in production environments like Google, Amazon, Microsoft, etc.. all you will have is command line.

Copyright Creative Commons Attribution 3.0 - Charles R. Severance