How to Use External Library in Python Programming Language

Posted on


So far, there are lots of built-in function available in python programming language. The built-in function is a function which is already exist and it is there without having to install anything. But in python programming language there are also lots of various functions, objects which is not by default embedded as a built-in function directly available from the python itself. Many people including programmers, developers, contributors made those functions and later on they proudly share those functions so that many people can get the benefit from those functions. The external library is the term to define those kinds of functions. There are many external libraries available in python. Those external libraries are very important for example to the significant use of data science such as numpy, panda, skit-learn and many others.

Installing External Library in Python Programming Language

In order to install those external libraries, the easiest step is just to use python package manager. In python programming language, the package manager is called ‘pip’. For an example, in order to install numpy, just execute the following step :

1. Run the command line interface. In the Windows operating system, just execute the Command Prompt.

2. After successfully run Command Prompt, just type the following command to install the external library using ‘pip’ package manager :

pip install package_name

There are several steps for installing ‘pip’ external library. In this context, the installation is in a python virtual environment. Just get into the virtual environment folder. For an example, in this context, the folder name is myenv. So, get into the folder as follows :

C:\python>cd myenv

C:\python\myenv>cd Scripts
Volume in drive C is Windows
Volume Serial Number is E003-3593
Directory of C:\python\django\myenv\Scripts
08/22/2020  04:01 PM    <DIR>          .
08/22/2020  04:01 PM    <DIR>          ..
08/22/2020  03:57 PM             2,284 activate
08/22/2020  03:57 PM               960 activate.bat
08/22/2020  03:57 PM            18,093 Activate.ps1
08/22/2020  03:57 PM               368 deactivate.bat
08/22/2020  04:00 PM           103,325 django-admin.exe
08/22/2020  04:00 PM               679
08/22/2020  03:57 PM           103,292 easy_install-3.8.exe
08/22/2020  03:57 PM           103,292 easy_install.exe
08/22/2020  04:01 PM           106,355 pip.exe
08/22/2020  04:01 PM           106,355 pip3.8.exe
08/22/2020  04:01 PM           106,355 pip3.exe
08/22/2020  03:57 PM           524,872 python.exe
08/22/2020  03:57 PM           523,848 pythonw.exe
08/22/2020  04:00 PM           103,278 sqlformat.exe
08/22/2020  04:00 PM    <DIR>          __pycache__
14 File(s)      1,803,356 bytes
3 Dir(s)   7,893,114,880 bytes free

After that, just execute the activate.bat file to activate the python virtual environment. Execute it as follows :


(myenv) C:\python\myenv\Scripts>

The command prompt format will change to inform that currently, the python virtual environment is active. After that, just execute the command for installing the external library as follows :

(myenv) C:\python\myenv\Scripts>pip install numpy
Collecting numpy
Downloading numpy-1.19.1-cp38-cp38-win_amd64.whl (13.0 MB)
|████████████████████████████████| 13.0 MB 56 kB/s
Installing collected packages: numpy
Successfully installed numpy-1.19.1
(myenv) C:\python\myenv\Scripts>

3. In order to use the external library in the python souce code, there is a condition for that library so that it is possible for further utilization. First of all, call or add the library using the available reserved keyword ‘import’. The following is an example for calling, adding and using an external library di the python. The following is an example :

Importing math external library

>>> import math

Defining the pi variable with the value from the attribute of pi from math package external library

>>> pi = math.pi

Defining the radius of the circle

>>> r = 2.5

Calculate the circumference and print it

>>> c = 2*pi*r
>>> print("Circumference: "+ str(c))
Circumference: 15.707963267948966

Calculcate the area and print it

>>> a = pi*r*r
>>> print("Area : ", str(a))
Area :  19.634954084936208

In the output above, calling the math library first and then using pi attribute exist in the math library.

Leave a Reply