A Short Python Script About The Distance Between Two Points

For this blog entry I wanted to do something different. During the past few weeks I’ve been working on a program that allows you to enter two points on an XY plane and find the distance between them. The formula to do this is pretty straightforward and can be calculated as follows:


So here is my little program, or a screen shot of it, at least. Christmas comes early this year! Woohoo :-p.


I wrote the distance formula program in Python,the programming language I described several weeks ago in a previous post. The interface is fairly intuitive (I hope!), so I don’t feel a readme file or a user manual is really necessary here: just enter the coordinates for your two points and click the Calculate  button(or just hit Enter). You can download the program here from my Google Drive account.

I will say something about launching the application, however. I hemmed and hawed a long time about how I should deliver this program and decided that it was best if I asked users to run it explicitly as a script through the Python interpreter rather than a standalone program.† The alternatives were to use shortcuts, which likely wouldn’t have worked in Windows (see Step 6 below), or to create executables for each kind of OS, which was an extra level of complexity I wanted to avoid given the application’s small size/usefulness.

So, to run the script, you need to make sure of the following. It might be a little extra work right now, but if you can get the script to work, the next time you want to run any other Python program will go much faster.

  1. Make sure you have Python 2.7.x or Python 3.x installed on your system. You can actually skip this step unless you are running Windows—Mac, Linux and other Unix OSs come with some version number of Python 2.7 out of the box.
  2. Learn how to navigate to the directory where the script is stored using your favourite shell, command-line tool, etc. using the cd command. In Mac, this is your Terminal app. Learning how use “cd” takes some practice, but it’s a skill which can be mastered in 15 minutes or less.§
  3. Download the Python script, dist_form.py. I’ve written the script for Python 2.7.x, so it won’t work in Python 3 unless you convert it with the 2to3 utility. Again, if you’re using a Mac or Unix-like system, you won’t need to worry about this.
  4. At the shell/command-line prompt, you can launch any functioning Python script by typing the following: python script_name.py. Or in our case, python dist_form.py
  5. You’ve started the program! Click on the application’s window to start using it. I tried for several days to make it so the program would get focus right away after launching it, but gave up when it seemed that I would need to fool around with the window management system, which (I think) is different for every operating system. Putting in Mac or Windows specific lines of code would have reduced the portability of the script.
  6. (*nix and Mac users only) If you want to speed up launching this or any other Python script, you can turn it into an executable by changing the permissions on the file. In your shell, type chmod +x script_name.py . You can then run the app either by double-clicking the file or, once you have navigated to its location in your shell, by typing at the prompt ./dist_form.py . You could also drag the file into your shell and hit Enter.

P.S. When you try to download the file, Google Drive will likely tell you “No Preview Available” and possibly suggest to view the file using a third-party app. Ignore this. Just download the file on your laptop/desktop. If you’re interested in seeing what the code looks like you can use your favourite text editor to open the file. I used TextWrangler to write the script.

*And the help of Tcl/Tk’s graphic user-interface libraries ported to Python.
† One basic distinction between a script and a standalone program is that you need another program to run the former (in this case the Python interpreter), but not the latter. I won’t get into the advantages/disadvantages of each lest I show my ignorance in this topic, which is regrettably vast.
‡ Read Step 5 for an example of the extra level of complexity I’m talking about.
§ I have no idea how long it will take you to learn the cd command, so take as much time as you need. It’s worth it ;-)!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s