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:
math
, random
, turtle
, winsound
)adventurelib
, guizero
, pgzero
)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:
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:
Convert a string to ASCII Art:
Installation: pip install pyfiglet
Documentation: https://docs.pytest.org/
Code samples:
Installation: pip install pytest
Documentation: https://pyttsx3.readthedocs.io/
Code samples:
Installation: pip install pyttsx3
Documentation: https://docs.python.org/3/library/random.html
Code samples:
Generate 5 random integers (ints) between 1 and 10:
Generate 5 random floating point numbers (floats) between 0.0 and 1.0:
Choose 5 random items from a list (with repetitions):
Shuffle a list (in place):
Installation: This is a standard library so it comes with Python already.
Documentation: https://docs.python.org/3/library/winsound.html
Code samples:
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:
App
class with the guizero.App
syntax:
Installation: This is a standard library so it comes with Python already.
gz
, and use the App
class with the gz.App
syntax:
App
class directly:
App
class directly:
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:
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).