Modules are a pre-written set of instructions or library of commands and operations which are already in their best current form and PIP helps us manage them. Modules basically make our work easier and faster as the models are pre-built we need not recreate the model.
We can obviously improve the modules but they are already in their best current form to use. Some modules are downloadable and some come built-in with the Python Interpreter. You can check the list of built-in modules for your python version here.
What is PIP?
Pip is a package manager which is used to download and install various python modules other than the built-in modules. It comes along with the python interpreter.
There are some packages like Virtualenv and Anaconda which are used to create environments. Both are quite similar in their working principle. They help us manage packages inside a virtual environment and isolates your workspace from the rest of the works.
Anaconda comes with its own package manager named conda from where you can download various other python libraries.
How to Check the Version of PIP and Upgrade PIP
To check the version of PIP currently installed in your system you can write the following code in your PowerShell. You must have the Python interpreter installed.
Now, to upgrade the version of pip you can paste the following code into your PowerShell terminal:
python -m pip install --upgrade pip
This will upgrade the package manager with the latest useable version available. Also, if you install a python module with an older version of PIP then it will automatically provide a warning for you to upgrade the PIP version, you can also copy the code from there to upgrade the PIP package manager.
How to Install and Upgrade Modules using PIP
Now to install any desired python packages or modules you can simply write a simple install command in PowerShell window including the module’s name like:
pip install matplotlib
Here I want to install ‘matplotlib‘ module. If you wish to install other modules like flask, pandas, etc. you need to replace the module’s name with the desired one.
Now to upgrade the already installed python module with the latest version you can write the code as shown below in your PowerShell window:
pip install matplotlib --upgrade
Replace the name of the module ‘matplotlib’ with your desired module, and the command will upgrade the existing module with the latest version of the same.
If you have any difficulty understanding the text above, you can watch this video by CodewithHarry. He has explained the concept in a very lucid and easy to understand language with examples.
How to Use Modules in Python
Before using any module in a python project, be sure that you do not name the python file with the name of any module used in the file, it will throw an error.
Now, we will use a python module named ‘playsound‘ to play an audio file stored on our device. To do so you need to first copy the path of the audio file. In Windows 10, you can copy the path of any file by the following steps:
- First, open the file explorer and select the audio file.
- Then from the navigation menu select ‘Home‘ and then click on ‘Copy Path‘ to copy the path of the selected file.
Now, we have copied the path of the audio file we want to play, so let us get to our VS Code to write our first Python program using modules. If you have not set up the VS Code for Python you can follow this guide to set it up quickly.
Here, we will use the ‘playsound‘ module to play an audio file (.mp3 file).
from playsound import playsound playsound("C:/Users/Ajay Choudhury/Downloads/Bheegi Bheegi - Neha Kakkar.mp3")
To run the above program, you can click on the green run button on the top right in VS Code, it will run the python code and in output will play the song. To stop the song press Ctrl + C.
Similar to ‘playsound‘ there are tons of other python modules available which can be used to accomplish interesting tasks like read out documents and PDF files, create a voice assistant, make a photo editor and many more.
Hope the guide helps. Feel free to put your suggestions, queries and comments below. You can also explore our study resources on YouTube. Subscribe if you like! Thank You!