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

(singke) #1

  1. At the interactive shell prompt, type import sys and press Enter to import the
    sys module.

  2. Type sys.path and press Enter to display the directories currently in the Python
    path. Do you see the /usr/local/lib/pythonversion/site-packages
    directory? If yes, you can skip step 22. If you don’t see this directory, move on to
    step 22.

  3. Type sys.path.append('/usr/local/lib/pythonversion/site-
    packages') and press Enter to add the production directory to the Python path.
    Remember to type the version number you found in step 15 rather than version.
    With the production directory containing the shopper.py module file added to
    Python’s path, you are ready to test the production custom module.

  4. Import the custom shopper module by typing import shopper and pressing
    Enter.

  5. Test the function discount by typing shopper.discountp(100,10) and
    pressing Enter. You should see the response 90. Congratulations! Your custom
    module has been tested and confirmed!

  6. If you want to power down your Raspberry Pi now, type sudo poweroff and
    press the Enter key.


By the Way: Proud of a Module You’ve Created?
You can be a part of the Python community by sharing modules you have
created. Start the sharing process by reading about Distutils at
docs.python.org/3/distutils/index.html. Also, take a look at github, github.com,
where you can share your work.

Understanding how to create a custom module is very useful in your Python learning adventure. Just
remember to follow the seven steps described in this hour, and you will be able to create lots of
custom modules containing your homemade functions.


Summary


In this hour, you read about finding and using modules in Python. You also read how to create your
own custom modules. The various Python module flavors were covered, along with where to find
modules stored in .py files on your Raspbian system. Also, you have learned where custom Python
modules can be stored. You got to try out creating your own custom module and moving it to a
production location. In Hour 14, “Exploring the World of Object-Oriented Programming,” you will
investigate object-oriented programing, including a concept called classes, which can also be stored
in a Python module.


Q&A


Q. I heard a Python expert talking about the “cheese shop.” What is that?
A. The “cheese shop” is the Python Package Index (PyPi), where you can find lots of Python
Free download pdf