Video modules in Python. In this video, we will learn about modules, how to import them, how to work with them. And finally, we will learn to install Python packages from Raspbian archives. And with the PIP tool, there is no point in reinventing the wheel. It is true in the world of programming that you can stand on the shoulders of giants by importing other programmers code seamlessly. By using the concept of modules and Python.
Your programs can import code created by other people using the import statement. This enables you to import modules and use their functions. The only difference is that they are now called methods instead of functions. To use the module in a program. First, you'll need to import the module to your current program. Furthermore, you will need to import all the modules needed for the program first itself in the code code.
A common module used in many programs is the math module. This allows you to access lots of math methods, which allows you to do factorial, truncation, power, logarithm, trigonometric operations, angular conversion, and much more. You can do a quick Google search to see all the available methods of any Python module. First, let's try out modules in the shell. In the shell of Tawny ID, enter input math, you now have access to all the methods in math, you won't notice any difference, but if you type the type of math, it will say that it is a module class. Now let's try to access the methods aka functions of the module.
This is done by the dot operator type math, followed by a dot and the name of the function you want to use inside the module type. math.sq rT of 16 which will give you four. Similarly, some methods can take more than one argument. For example, typing math dot POW 64 comma three will return the 64 raised to three, which is equal to two six to 144 point zero. Just like accessing methods from a module. You can also access constants from a module, which are fixed variables contained in the module by just replacing the function after the dot operator with the constant name.
For example, you can type math.pi to get the value of pi to 15 decimal places. Do you feel that typing math dot then the method name every time is time consuming? Then you can use the from keyword to import methods and constants directly to your program. This alerts the need to use math dot h Every time I open a new shell and type the following three instructions from math import pi, from math input a, from math input POW. Now if you type A or pi, or POW 64 comma three, you will get the output. Do you know that you can create your own modules and reuse them in your code?
By creating your own modules, and importing those created by other people, you can vastly improve the capabilities of your code. If you want to know more about creating your own modules, please visit the links in the resources. It's true that with Python, you can build just about anything from simple scripts to full applications. The Python language, however, doesn't come pre installed. With all of the fancy features you might want or require in the Raspberry Pi. The preferred method to install Python packages is from the Raspbian archives as it means That the modules you install can be kept up to date easily with a usual sudo apt update and sudo apt full hyphen upgrade commands.
To install a package to your Python. From the Raspbian archives, you can use apt. For example, let's install a Python package for working with the PI camera as an example. First, you need to update the archive list with sudo apt update when you type sudo apt install Python three hyphen pi camera to install for Python two version, you'll need to replace Python three with just Python. Not all Python packages are available in the Raspbian archives. And those that are can sometimes be out of date.
If you can't find a suitable version in the Raspbian archives, you can install packages from the Python package index. To do so use the PIP tool the tool comes pre installed in Raspbian, except on lite version. Let's install a GI toolkit package called gi zero as an example, to install g visor package using the pivotal type sudo PIP three, install g zero for Python two version, replace PIP three, with just Pip. You can uninstalled Python modules with sudo PIP three uninstalled, followed by the module name. If you have created some useful or cool modules and want to share them with others, you can upload it to the Python package index. Please check out the resource section to learn how to do it.
Summary. In this video we have covered the following why use modules, how to work with Python modules, installing Python packages using Raspbian archives and the PIP tool in the next video, we will learn to create a graphical user interface with Python.