Scientific computing with Python

by Conor Lawless email: conor.lawless@ncl.ac.uk

Executing Python scripts

Hello world!

Now that we have a script written (myscript.py), the next question is how to make it run? There are many ways to do this:

  1. Run code from within the IDLE GUI (useful for debugging code)
  2. Double-click on the script icon in Windows (fast & easy way to execute a script)
  3. Run code from the command-line (useful for preserving messages from Python, or executing scripts remotely or on other operating systems)

1. Run code from within the IDLE GUI

To run a script from within the IDLE GUI: if the script is not open, then right-click on the myscript.py icon, and choose "Edit with IDLE". Once the script is opened in the IDLE text editor, to run it, just go Run -> Run Module (or as a shortcut, you can press F5 instead). If the script has been modified since it was last saved, IDLE will prompt you to save the script before running it. This is good practice. If Python crashes while trying to run the script, you won't lose your work.

Hopefully you will see that Python has printed out "Hello World!" to screen as you have told it, and is waiting for further instructions. For this very simple command, you could just type the following into the shell to achieve the same effect:

print("Hello World!")

Typing short commands into the shell directly, executing code line-by-line is useful for trying things out, and we'll do some more of that later.

2. Double-click on the script icon

If Python is installed correctly under Windows, double clicking on a .py file will execute the script. However, for this trivial example, Python will open a terminal window, print the message the the window and close the window immediately. This will likely happen so fast that you will not have a chance to read the message! Nevertheless, this can be a very useful method to execute more complicated scripts that take longer to run, or whose output can be verified without inspecting the console: the creation of a new file on your hard drive, for example.

3. Run code from the command-line

Running code from the command-line is useful for doing computation remotely on another computer, or for automating the simultaneous execution of several scripts on your own computer. In fact, if you are familiar with using the command-line, it is often a quicker way to interact with computers in general. To run Python code conveniently, the Python executable should be in your PATH (a list of directories on your computer where useful, commonly used programs can be found). Your PATH should already be set up correctly on Linux and OSX.

After selecting "Add Python to PATH" during installation, you should be able to open a terminal or console (e.g. for Windows Start->All Programs->Accessories->Command Prompt) and enter the command python at the prompt (e.g. for Windows C:\> python). Entering the python command in your terminal will load a stripped-down version of the Python interpreter we've seen already, which should look something like this:

Python 3.5.11  (default, Apr 10 2016, 14:24) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or license for more information.
>>>

If you neglected to add Python to your PATH during Windows installation, there are some instructions how to do this manually here.

As before, you can enter Python commands line-by-line. You can exit the interpreter by entering quit().

To be able to execute a script from the command line, the script should be found in the current working directory of your terminal or console. In Windows, the easiest way to do this is to open a command prompt in the directory you've already saved the "myscript.py" file. There are several ways to do this, but probably the easiest is described here: right-click on a directory while holding down the Shift key, before choosing "Open command window here". Alternatively, you can navigate to a new location from any terminal window.

Provided that the "myscript.py" file is located in the current working directory, you can enter the following command in your terminal to execute all of the commands written in "myscript.py":

python myscript.py

Similarly, you can also execute the script by entering the following from within the Python interpreter:

>>>import myscript

Some more complicated, flashy demo code

OK, so now you know a few different ways to execute a Python script, save the script below (don't forget the ".py" extension) and try to use each method to execute it, and see what it does. Don't worry about how it works for the moment. Later on we will see how you might build up code to create a complicated pattern like this from simple building blocks: which is the essence of programming.

import turtle
john = turtle.Turtle()
john.speed("fastest")
john.hideturtle()
john.pensize(2)

def polygon(side,ngon,colour="black",skip=1):
  john.color(colour)
  for i in range(ngon):
    john.forward(side)
    john.right((360.0/ngon)*skip)

def polycircle(side,num,ngon,colour="black"):
  for i in range(num):
    polygon(side,ngon,colour)
    john.right(360.0/num)

def polycircles(side,num,ngon,bigRadius,numCircles,colours=["black"]):
  for i in range(numCircles):
    col=colours[i%len(colours)]
    john.up()
    john.forward(bigRadius)
    john.down()
    polycircle(side,num,ngon,col)
    john.up()
    john.backward(bigRadius)
    john.right(float(360)/numCircles)
    john.down()

for i in range(1,10):
  polycircle(side=10*(1+i),num=10,ngon=i+2,colour="black")

polycircles(side=50,num=10,ngon=5,bigRadius=300,numCircles=10,colours=["blue","red"])

OverviewInstallationFirst ScriptExecutionLibrariesStructureOther Resources


Last updated: April 2016