Programmers often have to deal with large amounts of code which can get confusing. We also usually use the same set of functions, methods, conditional statements, etc., more than once in a program. Python modules tell us where to write Python code that we can import into a program.
As these modules grow in number, it becomes crucial to store them based on their similarities. Python packages offer a module namespace structure using dot notation.
What is PIP?
Pip is the basic package manager in Python that lets us install and manage any additional packages that are not available as part of the standard library. For example:
pip install pandas
If you have pip installed in your system, the above command will install the pandas package library.
How to Install PIP?
Package management has now become so crucial that all versions after 3.4 for Python 3 and 2.7.9 for Python 2 are available with pip manager included with the Python interpreter. The Python installer automatically installs pip for your use. However, if you are using an older Python version, you will have to install it separately. You can check if the pip package manager is installed by running the following command in your console:
pip --version
If pip is present in your system, you will see the pip version as follows:
pip 19.3.2 from C:\Python37\lib\site-packages\pip (python 3.7)
The output also displays the location and version of Python.
Using PIP
Since pip is a command-line program, we use the pip command with the command prompt once it gets installed. The syntax of pip is:
pip <arguments>
For example:Â
pip install django
The above example will install the Django library for our use.
Installing Packages with PIP
The Python standard library offers an extensive set of modules and packages that help us with script and application development. In addition, developers contribute even larger packages that can help you achieve your development needs.Â
These packages get created for various development frameworks, tools, and libraries. They are published and hosted on the Python Package Index, also known as PyPI. Most of the packages available on PyPI simplify Python development as they provide easy-to-use interfaces to functionality that already exists in the standard library.Â
Basic Package Installation
If we want to install packages in Python, we use the install command with pip. For example, if we want to install the pandas package, we use the following command:
pip install pandas
Output:
Collecting pandas
Using cached pandas-1.2.4-cp39-cp39-win_amd64.whl (9.3 MB)
Installing collected packages: pandas
Successfully installed pandas-1.2.4
In the above code, we use pip with the install command followed by the package name we want to install (pandas). One of the most popularly known packages in Python is the pandas package used for efficient data manipulation and analysis.Â
All the dependencies listed in the package metadata also get installed to ensure that the package has all the requirements it needs.
Specifying Package Version
When we use the pip install command directly, Python downloads the latest version of the package. However, if you wish to install a specific package version (to ensure it is compatible with your system), you can define the version as follows:
pip install pandas==1.2.4
The above command will install version 1.2.4 of the pandas library.
Listing Installed Packages with PIP
Now that we have installed the pandas package, we can use the list command to see the list of all packages installed in your Python environment.
pip list
Output:
Package Version
----------------- ---------
apipkg 1.5
certifi 2020.12.5
numpy 1.20.2
packaging 20.9
pandas 1.2.4
pip 21.1.1
py 1.10.0
pycodestyle 2.6.0
pylint 2.6.0
pyparsing 2.4.7
python-crontab 2.5.1
python-dateutil 2.8.1
pytz 2021.1
requests 2.25.1
setuptools 49.2.1
youtube-dl 2021.3.3
As you can see, pip got upgraded to the latest version (21.1.1) and the pandas version 1.2.4 was installed.
Package Information With pip show
As you saw in the above package list, there are many packages available in the Python environment. To see the metadata of one particular package, we use the pip show command as follows:
pip show pandas
Output:
Name: pandas
Version: 1.2.4
Summary: Powerful data structures for data analysis, statistics and time series
Home-page: https://pandas.pydata.org
Author: None
Author-email: None
License: BSD
Location: c:\users\sssah\appdata\local\programs\python\python39\lib\site-packages
Requires: pytz, python-dateutil, numpy
Required-by:
Notice the Requires and Required-by columns in the above output. The Requires column specifies all the dependencies required by the pandas library. On the other hand, the Required-by column displays the packages that require the pandas library.
Uninstalling a Package with pip
Similar to installing a package, we use the uninstall command with pip to uninstall a package. For example, if we want to uninstall the pandas package from the Python environment, we use the following command:
pip uninstall pandas
Output:
Found existing installation: pandas 1.2.4
Uninstalling pandas-1.2.4:
Would remove:
c:\users\sssah\appdata\local\programs\python\python39\lib\site-packages\pandas-1.2.4.dist-info\*
c:\users\sssah\appdata\local\programs\python\python39\lib\site-packages\pandas\*
Proceed (y/n)? y
Successfully uninstalled pandas-1.2.4
The pandas package gets successfully removed after the final prompt. It is crucial to remember that although the pandas library got uninstalled, the packages that were installed as dependencies were not.Â
In this case, dependencies like pytz, python-dateutil, and numpy are still present in the Python environment. To remove a package dependency, we will have to view installed packages using the pip show command and then uninstall them manually.
Using Requirement Files
You may want to create a specification of the dependencies and versions you will use to develop and test your application. Requirement files contain all the package names and versions that we wish to install.
Let us say we have a requirements.txt file that has the following packages.Â
numPy
requests
pandas
We can install all three packages and their dependencies with only one command as shown:
pip install -r requirements.txt
Output:
Collecting numpy
Downloading numpy-1.20.3-cp39-cp39-win_amd64.whl (13.7 MB)
Collecting requests
Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
Collecting pandas
Using cached pandas-1.2.4-cp39-cp39-win_amd64.whl (9.3 MB)
Installing collected packages: numpy, requests, pandas
Successfully installed numpy-1.20.3 pandas-1.2.4 requests-2.25.1
Although we used the same pip install command, the additional -r argument emphasizes to pip that we are passing a requirements file and not a single package name.
Creating Requirements File
Instead of manually creating a requirements file, we can also use the freeze command offered by pip to create a requirements file. Let us say our current Python environment has the following packages:
Package Version
----------------- ---------
certifi 2020.12.5
numpy 1.20.2
packaging 20.9
pandas 1.2.4
pip 21.1.1
py 1.10.0
python-crontab 2.5.1
python-dateutil 2.8.1
pytz 2021.1
requests 2.25.1
Now, we can list the packages that do not come preinstalled with Python by using the freeze command.
pip freeze
Output:
certifi==2020.12.5
numpy==1.20.2
packaging==20.9
pandas==1.2.4
py==1.10.0
python-crontab==2.5.1
python-dateutil==2.8.1
pytz==2021.1
requests==2.25.1
The above pip freeze command displays all packages and their respective versions according to the requirements file format.
You can redirect the output to a file to generate a requirements file as shown:
pip freeze > requirements.txt
The new requirements.txt file that gets created helps us install the exact requirements into another system. Although we name the file requirements.txt by convention, you can give it any other name.
Search Packages in Pip
Pip offers us a command called ‘search’ that we can use to find a specific package in the command prompt. For example, if we want to search for a library called numPy, we use the following command:
pip search numpy
When we run this command, a list of all the packages that match the keyword get displayed. We can use this command to find related packages since other packages having the keyword get displayed.
Questions and Answers
Q1. How do you write the first code in Python?
A. To write a Python code, you will first have to download the most recent version of Python. Once downloaded, run the installer file and complete the steps to install Python. If you want to be able to run Python from any part of your computer, choose the ‘Add Python to environment variables’ option during installation.Â
Once Python is installed, you can type ‘python’ in the command line to invoke the interpreter in immediate mode. You can then directly type in your Python code to get an output. For example, you can simply type 5 + 5 and hit Enter. You will get the output as 10. If you wish to exit the command line, type quit() and click Enter.
Q2. How do I write a Python script?
A. You can use any text editing software to write a Python script. All you have to do is make sure to save the file with a .py extension. However, using an IDE is more useful as it offers features like code hinting, file explorers, etc.
Q3. How do you write simple code in Python?
A. You can use the following steps to know how to write a simple Python code.
Step 1: Begin by going to your Start menu and choosing the Python command line. You will notice a prompt that looks like this: >>>. It shows you where to write Python code.
Step 2: Type in the following code and hit the enter key.
print('Hello Everyone!')
Output:
Hello Everyone!
Q4. Which software is best for Python programming?
The easiest and best way to run Python is through the Thonny IDE. As it is available with the latest version of Python, you will not have to install Python separately. Although you can give your Python files any name, remember that you need to save them with the .py extension for them to be valid.
Leave a Reply