Reader small image

You're reading from  Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Product typeBook
Published inSep 2015
Reading LevelBeginner
Publisher
ISBN-139781783551590
Edition1st Edition
Languages
Right arrow
Author (1)
Dan Nixon
Dan Nixon
author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon

Right arrow

Running some simple Python scripts


Now we will look at writing a Python script in a file and executing it. For this we will use IDLE as it will provide syntax highlighting on the code. However, any text editor (for example, LeafPad, GEdit, nano, vim) can be used to write the Python files.

  1. First open IDLE and select New Window from the File menu, as shown in the following screenshot. This will open a new text editor window which will allow you to write and edit the script files.

  2. Now type the following code into the editor. This is a simple script that imports the time module and prints a string containing the current time to the terminal.

    import time
    print "The current time is: " + time.ctime()

    Note

    Keep in mind that the indentation level in Python is very important as this defines the scope that a line of code fits into. This will become clearer later on in the book when we start writing more complex codes.

  3. Save the file as time.py in the home directory by selecting Save from the File menu.

    Now that the script is saved, we can execute it using the Python executable at the command line.

  4. Open a terminal and enter the following command to execute the Python script:

    python time.py
    

    This will give the following output to the terminal:

    One small improvement that could be made to this process is to include a shebang in the Python script that will tell the shell what to use to execute the script. This way we do not have to explicitly include the Python command when we run the script.

  5. Go back to the Python script in IDLE and add the following line as the very first line in the file:

    #!/usr/bin/env python
  6. Next, we need to give execute permissions to the file in order to execute it directly (that is, without calling the Python executable first). This is done using the following command in the terminal:

    chmod a+x time.py
    
  7. Now we are able to execute the script using the following command:

    ./time.py
    

    This gives the following output on the terminal:

Previous PageNext Page
You have been reading a chapter from
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
Published in: Sep 2015Publisher: ISBN-13: 9781783551590
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon