Setting things up

It is assumed that Python has been preinstalled. If not, please visit https://www.python.org/downloads/ and https://www.python.org/download/other/ for the latest Python version for your operating system. Regarding the general setup and installation procedure, please visit https://realpython.com/installing-python/ to find out how to install Python on your chosen platform. We will be using the Windows operating system here.

To verify that we have all the required tools available, let's check that Python and pip are installed and are up to date.

pip package management system is used to install and manage software packages written in Python. More on installing Python packages and pip can be found at https://packaging.python.org/tutorials/installing-packages/.

We will be using Python 3.7 on the Windows operating system. Press Windows + R to open the Run box and type cmd to get the command-line interface:

Opening the command-line interface on the Windows operating system

Now, move to your root directory and type the following command:

C:> python –version
Python 3.7.0

The preceding command will provide us with the Python version that we currently have on our system. Let's get some information on the pip version that we are using. The following command will display the current pip version, as well as its location:

C:> pip --version

pip 18.1 from c:python37libsite-packagespip (python 3.7)

We are happy to proceed after seeing the preceding responses. If you encounter an error stating Application not found or not recognized as an internal or external command, then we need to reinstall Python or check for the proper drive that was used during installation. 

It's always advisable to check for the system and library version and keep them updated unless a specific version is required.

To update pip to its latest release, use the following command:

C:> python -m pip install --upgrade pip

You can verify the libraries we wish to use, that is, requests and urllib, either from the command line or by importing the Python IDE and getting details on the package using the help() method:

C:> pip install requests

Requirement already satisfied: requests in c:python37libsite-packages (2.19.1)

As shown in the preceding code, we are trying to install requests, but the command returns Requirement already satisfied. The pip command checks for an existing installation on the system before installing a fresh library. 

In the following code block, we will be using the Python IDE to import urllib. We'll view its details using Python's built-in help() method.

The >>> symbol in code represents use of the Python IDE; it accepts the code or instructions and displays the output on the next line:

>>> import urllib 
>>> help(urllib) #display documentation available for urllib

The following is the output:

Help on package urllib:
NAME
urllib
PACKAGE CONTENTS
error
parse
request
response
robotparser
FILE
c:python37liburllib\__init__.py

Similar to the previous code, lets import requests using the Python IDE:

>>> import requests   
>>> requests.__version__ #display requests version

'2.21.0'

>>> help(requests) #display documentation available for requests

Help on package requests:
NAME
requests
DESCRIPTION
Requests HTTP Library
~~~~~~~~~~~~~~~~~~
Requests is an HTTP library, written in Python, for human beings.

 

If we import urllib or requests and these libraries don't exist, the result will throw an error:

ModuleNotFoundError: No module named 'requests'

For missing modules or in the previous case, install the module first; use pip as follows to install or upgrade. You can install it from your command line, as follows:

C:> pip install requests

You can also upgrade the module version using the --upgrade argument:

C:> pip install requests -–upgrade
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.249.178