In this section, we will continue what we started in the previous section, and we are going to focus
on Python and the terminal.
As a reminder, you can download a recap PDF with all the commands at the end of the last section.
And here in this lesson we will see how to install Python modules.
So you've already seen a few Python modules for example time and signal that are already part of the
Python three built in libraries.
Then you have some additional modules, for example the Gpio zero, which is not part of the built in
libraries, and that you need to install manually.
So actually Gpio zero, you don't need to install it because it's already included when you install
Raspberry Pi OS.
Okay.
So the kind of install it for you.
And so here we're going to see how to.
Well no actually what Python modules you have installed and how to install new ones.
We're going to do that from the terminal.
But know that there is also an option to do that from Thonny, although I don't really recommend it.
So on Thonny you have tools and then manage packages, and here you can see a list of packages and you
can for example, type the name here Gpio zero.
Let's search.
You find the Gpio zero here and you see well, it's already installed, but if it was not installed
you could choose to install it.
But here and also as best practice, I would recommend not to install modules from here and just to
do it from the terminal.
Okay.
So I'm going to close Tony.
So how to list all the Python modules that you have while you have this command Pip3 that will allow
you to manage Python modules.
And then you can do pip three space list, press enter.
And you see you have a long list of Python modules.
So those are all the ones that are installed.
And for example we can find.
So that's going to be here.
You can find Gpio zero.
So you have the name of the module and also the version number.
So it might be different for you.
But at least you can see all the names with all the version numbers for all the Python modules that
you have.
Now if I come back here and if you actually want to search for a specific module, I'm going to give
you a quick tip.
So you do pip three list.
Then you can do pip and after pipe you put grep okay.
This is going to filter depending on what you put here.
So for example Gpio.
This is going to filter the result of that command and only outputs the lines that match with this here
on the right.
So here you can see where we have a few things.
But you can see we have the Gpio zero right here.
So it's a bit easier to find it.
Great.
So with this you can know what libraries you have.
You can find a specific library and see what version you have for that library.
Now how to install a Python module.
So note here I use library and module.
This is basically the same thing.
Just that in Python three.
Usually we use the term module I'm going to clear and how to install a Python module.
Well you have two ways.
And the way that we're going to prefer here is with apt.
So we're going to do sudo apt install.
And then you're going to start with Python three dash and then the Python module.
So if it's available you can install it.
Not all Python modules would be available for installation with APT, but if it's there you can install
it for example Gpio zero.
And note here I'm going to press tab.
And you see it's actually recognised means that it exists.
So I'm going to press enter.
And what you can see what's already installed.
So nothing new.
So that's how you can install globally a Python module.
And I would recommend to use that every time you can, especially on the Raspberry Pi.
Then an alternative is to use Pip3.
So I'm coming back to this pip3 command.
This time we've installed.
And here you don't need sudo in front.
So pip3 install and then you put the name.
So just Gpio zero okay.
Press enter and you might see this error okay.
Error externally managed environment.
And this is because now while pip3 is a bit more strict and they want you to create a virtual environment
and then to only install the packages in that environment.
Or you can see they also recommend you to install Python packages here with apt install Python three
module, which is exactly what we did here.
But if you still want to use pip three, then you still want to install the package in an easy way,
especially if you work on the Raspberry Pi, there's going to be much easier.
You can add this option here.
Break system packages.
So I'm going to run the same command and then dash, dash brake system packages.
You can just copy and paste also from here if you want.
Okay.
So I press enter and now it's going to work.
And what he is not going to install anything because it was already installed for me.
All right.
So that's how you can install a Python package.
Again I will recommend that you use the apt install if possible.
And then well we have a package.
How to remove it I'm going to do clear.
Well if you have installed it with apt you can remove it with apt.
So sudo apt remove and then the name of the package.
For example Python three dash gpio zero.
I'm not going to do it okay.
But basically it's the same thing as what we did before with normal packages.
And then you can also do pip3.
And this time the command is not remove but it's uninstall.
So tiny difference.
And then Gpio zero.
And this is going to uninstall the package with pip three.
And well I'm not going to run this command as well.
All right.
And with that you have everything you need to install new Python modules.
And to finish all the projects for this course.