Python libraries

A Python library, also known as module or package, is a collection of constants, functions and classes that can be reused when writing new programs.

There are three types of libraries:

Libraries used at Code Club

Here are all the libraries that we use at Code Club:

Documentation: https://adventurelib.readthedocs.io/

Code samples: See Build an adventure game with Adventurelib for details.

Installation: pip install adventurelib

Documentation: https://lawsie.github.io/guizero/start/

Code samples: See Build an app with Guizero for details.

Installation: pip install guizero[images]

Documentation: https://inflect.readthedocs.io/

Code samples:

import inflect engine = inflect.engine() number = 123 words = engine.number_to_words(number) print(number, 'is', words) # Output: # 123 is one hundred and twenty-three

Installation: pip install inflect

Documentation: p5-index.html

Code samples: See Create an animation or digital art with P5 for details.

Installation: No setup required! Use the online code editor from the Raspberry Pi Foundation.

Documentation: https://pygame-zero.readthedocs.io/

Code samples: See Build a video game with Pygame Zero for details.

Installation: pip install pgzero

Documentation: https://pypi.org/project/pyfiglet/ or https://www.tutorialspoint.com/ascii-art-using-python-pyfiglet-module

Code samples:

List all the possible font names:

import pyfiglet font_names = sorted(pyfiglet.FigletFont.getFonts()) print(len(font_names)) print(font_names) # Output: # 549 # ['1943____', '1row', '3-d', '3d-ascii', '3d_diagonal', '3x5', '4max', '4x4_offr', '5lineoblique', ...

Convert a string to ASCII Art:

import pyfiglet text = "Code Club!" font = "speed" ascii_art = pyfiglet.figlet_format(text, font) print(ascii_art) # Output: # _________ _________ ______________ ______ ______ # __ ____/___________ /____ __ ____/__ /___ ____ /____ / # _ / _ __ \ __ /_ _ \ _ / __ /_ / / /_ __ \_ / # / /___ / /_/ / /_/ / / __/ / /___ _ / / /_/ /_ /_/ //_/ # \____/ \____/\__,_/ \___/ \____/ /_/ \__,_/ /_.___/(_)

Installation: pip install pyfiglet

Documentation: https://docs.pytest.org/

Code samples:

# Under construction...

Installation: pip install pytest

Documentation: https://pyttsx3.readthedocs.io/

Code samples:

import pyttsx3 text = "Hello you! What's up?" pyttsx3.speak(text)

Installation: pip install pyttsx3

Documentation: https://docs.python.org/3/library/random.html

Code samples:

Generate 5 random integers (ints) between 1 and 10:

import random for i in range(5): print(random.randint(5, 10)) # Possible output: # 7 # 10 # 9 # 7 # 8

Generate 5 random floating point numbers (floats) between 0.0 and 1.0:

import random for i in range(5): print(random.random()) # Possible output: # 0.44989440807025205 # 0.7719221475578524 # 0.8117074999965667 # 0.3216176959290542 # 0.8296494377175666

Choose 5 random items from a list (with repetitions):

import random fruits = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"] for i in range(5): print(random.choice(fruits)) # Possible output: # date # elderberry # apple # elderberry # grape

Shuffle a list (in place):

import random vegetables = ["artichoke", "beet", "carrot", "daikon", "eggplant", "fennel"] print("Before:", vegetables) random.shuffle(vegetables) print("After: ", vegetables) # Possible output: # Before: ['artichoke', 'beet', 'carrot', 'daikon', 'eggplant', 'fennel'] # After: ['eggplant', 'carrot', 'artichoke', 'beet', 'daikon', 'fennel']

Installation: This is a standard library so it comes with Python already.

Documentation: https://docs.python.org/3/library/winsound.html

Code samples:

import winsound frequency = 440 # 440 Hertz duration_ms = 500 # 500 milliseconds = 0.5 second winsound.Beep(frequency, duration_ms)

How to use a library?

To use a library in your program, you must import it. There are different ways to do this. Let say you want to use the App class from the guizero library. You can choose one of:

Import the whole library and use the App class with the guizero.App syntax: import guizero ... app = guizero.App(title="My app") ...

Installation: This is a standard library so it comes with Python already.

Import the whole library with an alias, let say gz, and use the App class with the gz.App syntax: import guizero as gz ... app = gz.App(title="My app") ... Import just what you need from the library and use the App class directly: from guizero import App ... app = App(title="My app") ... Import everything from the library and use the App class directly: from guizero import * ... app = App(title="My app") ...

Common errors

Here are some common errors that you may encounter when using libraries:

Error message: ModuleNotFoundError: No module named 'xyz'

Reason #1: The library xyz is not installed on your computer.

Solution #1: Install the library xyz on your computer. This can usually be done with pip install xyz. Your IDE may also provide an easier way to do this.

Reason #2: The library xyz is not in PYTHONPATH (the list of folders where Python searches for libraries).

Solution #2: If you are in a school, you may have to add two lines like this at the beginning of your program:

import sys sys.path.append(r"\\dwstr04\Student Shared\CodingClub\python-packages")

Reason #3: You have mispelled the name of the library in the import statement.

Solution #3: Check the name of the library and use the correct spelling. Remember that Python is a case-sensitive language (ie upper case letters are different from lower case letters).

Error message: NameError: name 'xyz' is not defined

Reason: You have mispelled the name a constant/function/class from the library.

Solution: Check the name of the constant/function/class and use the correct spelling. Remember that Python is a case-sensitive language (ie upper case letters are different from lower case letters).