Main Menu

search

You are here

Python Home page

last updated: 2023-09-04]
rPi home page
rPi programs:
(link to:) python.org tutorial
(link to:) W3school tutorial
-----
Python version:
Using pip to install Python packages:
Python Programming:
----------


Python is one of the programming languages that can be conveniently used on the Raspberry Pi or any other Linux platform.

    On this Page:
  • General Considerations:
  • Creating/Editing a Program:
  • Executing a Program:
    • from command line:
    • from inside another program
    • execute automatically at startup/boot
  • Installing python packages:

----------

  • General Considerations:
    • Is python installed?
      Verify by requesting the version number.

    • Python programs are simple text files with a .py extension.
      As such they can be created and edited with any text editor.
    • Gotchas:
      • Python is different than many other common programming languages in some key respects:
        a specific protocol for indentation must be followed.
        Briefly, while some other languages might use "( )" to enclose blocks of code, Python uses indentation.
        See examples in "Python Statements" page.
      • There is no "loop" in Python. A program runs through one time, then stops.
      • Comment lines are designated with a "# " (pound/hash + space) at the start of a line that will be ignored during execution.
      • There is no "block comment" in Python. Whereas eg. Arduino has "/* & */" to denote a block of comment lines,
        in Python you have to put a "# " at the beginning of every line in your block of comments.

  • Creating/Editing a program:
    • Python programs are text files. Use any text editor to write them.
      They must have a ".py" extension.
      When you save your program, select "Save As" so you can specify a ".py" extension,
      or else rename the file after editing to change the extension from .txt or whatever to the required .py extension.
      • The rPi nano program is a built-in program on the rPi that nicely highlights different line types with different colors,
        so eg. comments are one color and executable program commands are another.
          Invoke the nano editor with a command like this:
          $ sudo nano [path/filename.py]
    • The first line of your program should be:
      #!/usr/bin/python
      This is called the "shebang", and tells Linux where to look for the interpreter (/usr/bin)
        You can use #!/usr/bin/env python3 for portability across different systems in case they have the interpreter installed somewhere else.

        Not sure about specifying ...python vs. python2 vs. python3. They all nominally worked in my trials...

    --------------------------------------------

  • Executing a program:
    • From a command line in a terminal:
      • execute:
        $ sudo python [complete path/filename (including the .py)]
        -or- if using python3:
        $ sudo python3 [path/filename.py]

      --------------------------------------------

    • From inside another program:
      • There surely are several ways to do this, and not all of them will work with all versions of Python. but here are a few:
      • with os.system
          import os
          os.system( 'python sub.py' )

        [go to: Linux Statements to see what version of Python you're running]
        --------------------------------------------

      • treat it like a module with import:
        • suppose you want to run main.py, and you want to execute sub.py from within main.py
        • add this line to main.py:
          import sub
          (note you do Not include the .py)
        • when main.py runs, it will execute sub.py one time
          in the process of importing it.
        • If sub.py contains function definitions,
          you can execute them from within main.py with:
          sub.[sub.py_funcName]

        --------------------------------------------

      • treat it like a module with from ... import *:
        • per previous example, add this line to main.py:
          from sub import *
        • Now you can execute any functions defined in sub with:
          [sub.py_funcName] ( )

        --------------------------------------------

      • with exec:
        • to execute sub.py from main.py,
          put this line into main.py:
          exec(open('sub.py').read ( ))
          • OR:
            execfile('sub.py')

          Note: both these methods worked with my python 2.7.15+
          However, execfile fails in python 3.5.3, though exec(open... still works.

        • clever option ...
          def run(runfile):
            with open(runfile,"r") as rnf:
              exec(rnf.read())

          Syntax:
          run("file.py")

      --------------------------------------------

    • running a program automatically at startup:
      • Again, there are several ways to do this, depending on which version of python you're running:
        but here's how I'm doing it using a script file (.sh) in rc.local:
      • Add a line in rc.local to directly execute your python program:
        The rc.local file is located in the /etc directory.
        (see Using a script file link above (???) for rc.local editing instructions)
        Add this line at the end of the rc.local file, just above/before the 'exit 0' line:
          $ sudo python [path/filename.py] &
          The "&" must be included so that the python program will run in the background and the rPi will continue to boot and run as normal

      --------------------------------------------

    • Installing python packages:
      • If you want to install a new package, do this:
        for python 3.x: $ sudo python3 -m pip install [packageName]
      • If you do not have pip installed,
            (check with: $ pip --version)
        get it with:
        $ sudo apt install python-pip

      ---------------------------------

    .

    .

    .

    eof