Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

  1. Write out the information from the text editor to the module by pressing Ctrl+O. The
    module file name should appear along with the prompt File name to write.
    Press Enter to write out the contents to the shopper.py module.

  2. Exit the nano text editor by pressing Ctrl+X.

  3. Now that the shopper.py custom module is created, test it by typing python3
    and pressing Enter to enter the Python interactive shell.

  4. Now import the os module by typing import os and pressing Enter. You need
    this module to change the present working directory within the Python interactive
    shell.

  5. Type os.chdir('/home/pi/py3prog') and press Enter. Python changes
    your present working directory to /home/pi/py3prog, where the shopper.py
    module file is located. (You can double-check your present working directory by
    typing os.cwd() and pressing Enter.)

  6. Now see if you can import your new custom module by typing import shopper
    and pressing Enter. Does Python give you an error? If so, you need to go back to step
    4 and try again to create the file shopper.py. If you get no errors, you can
    continue on to the next test.

  7. Test the function priceper, which is stored in the shopper module, by typing
    shopper.priceper(3,12) and pressing Enter. You should see the result 4.
    Test the rest of the functions in the module.

  8. Press Ctrl+D to exit the Python interactive shell and begin the process of moving the
    shopper module to a production directory.

  9. At the Raspbian shell prompt, type python3 -V and press Enter. This allows you
    to determine the current version of Python you have on Raspbian. (You learned about
    this is Hour 3.) You only need to know the first two numbers of the version number
    displayed (for example, 3.2) for the next step

  10. You must check and see if the production directory currently exists on your
    Raspberry Pi by typing ls /usr/local/lib/pythonversion/site-
    packages (where version is the version number from step 15, such as
    /usr/local/lib/python3.2/site-packages) and press Enter. If you do not get an error
    message after pressing Enter, you can skip step 17. If you get an error message, that
    is okay; simply fix the problem in step 17.

  11. Type sudo mkdir /usr/local/lib/pythonversion/site-
    packages and press Enter. Remember to type the version number you found in step
    15 rather than version.

  12. Move your custom module, shopper, to the site-packages directory by
    typing sudo cp /home/pi/py3prog/shopper.py
    /usr/local/lib/pythonversion/site-packages and pressing Enter.
    Be careful here! The command may wrap on your screen (and that’s okay). Again,
    remember to type the version number you found in step 15 rather than version.

  13. Now you have finished moving the module to a production directory, type
    python3 and press Enter to reenter the Python interactive shell.

Free download pdf