Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

You're reading from   Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications) Learn to design and implement reliable Python applications on the Raspberry Pi using a range of external libraries, the Raspberry Pis GPIO port, and the camera module

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781783551590
Length 200 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Dan Nixon Dan Nixon
Author Profile Icon Dan Nixon
Dan Nixon
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Your First Steps with Python on the Pi FREE CHAPTER 2. Understanding Control Flow and Data Types 3. Working with Data Structures and I/O 4. Understanding Object-oriented Programming and Threading 5. Packaging Code with setuptools 6. Accessing the GPIO Pins 7. Using the Camera Module 8. Extracting Data from the Internet 9. Creating Command-line Interfaces 10. Debugging Applications with PDB and Log Files 11. Designing Your GUI with Qt Index

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.
    Running some simple Python scripts
  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:

    Running some simple Python scripts

    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:

    Running some simple Python scripts
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 2015
Publisher:
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.
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 $19.99/month. Cancel anytime