The must-know command in Python — pip

Spread the love
The must-know: pip install - sometimes a hero like Python does need help from others
The must-know: pip install – sometimes a hero like Python does need help from others

The must-know in Python

Python is a powerful language which can do almost everything and be used by top tier companies/organizations like NASA, IBM and Google.

But wait, we are not going to make something big like those companies. Not today, remember, we are just a 10 seconds coder at this moment.

However even a 10s coder, we still have our thing to do for our great coding journey. Here we go: to learn the must-know command in Python — pip .

“Pip” is a package manager that helps you install packages in Python. Likes superhero comics, sometimes a great superhero does need help from his/her allies. So you can think in this way:

pip is the “Justice League” / “Avengers” to Python.

When we ask Python to do certain operations which are not under its core functional libraries, we need to use “pip” to add extra packages to Python. In the superhero scenario, when a hero need some advice on physics, he/she can seek Dr. Bruce Banner for help from the Avengers (of course, you can only do this before Dr. Banner “turning green” ).

Hands-on Section

We talk and we walk, let’ see how does the pip work with Python. Open the command prompt terminal and type the following command:

Avengers Assemble!

No way, just kidding. The proper syntax is:

pip install SomePackage

Which SomePackage is the package we would like to install into Python (i.e. the Dr. Banner we mentioned above).

If your machine cannot find ‘pip’, you may need to make sure the pip is downloaded from installers or OS commands. Then you can run following command to ensure pip is connected to your Python environment:

python -m ensurepip

We can now go to our Python environment and write a program to get web site status. Since there is no core module in Python handling http request, we have to import the “requests” module by typing

import requests

Then following results appear:

Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'requests'

Yes, Dr. Banner, I mean, the ‘requests’ module, is not here. As we just didn’t use the pip command to install it into Python environment. What we need to do is, run the ‘pip install requests’ command on command terminal (not the Python environment):

pip install requests

Once the package installation is finished, it will pop out:

Successfully installed requests-2.18.1

Now we are good to go. We type the same code again and let it query the Python official web site and an invalid page:

import requests

The results should be:

Response [200]
Response [404]

We did it! We have added new package on Python and let it handle http requests. And don’t forget, we have just learnt to write a python program to read http status!

By adding other packages on Python, we can make it handle different operations, expand its functionalities and aim for something big.


What we have learnt on this hands-on:

  1. the usage of pip command
  2. a sample program to get http status