Where are Python Libraries
Summary : Standard libraries are how the Python functionality is extended beyond the basic syntax available. From basic mathematical operations all the way to OS level operations on files and memory, web interactions like web-services, HTML parsing etc can all be done directly in core python using standard libraries provided as part of basic python installation.
What are Python Standard Libraries
At its core any language basically consists of just variables, data structures, loops etc. But a computer language is much more right ? For example, where do you go to perform operations like math, IO, File operations, text processing etc ? You take the help of libraries. In a language like C, you include the relevant library using # include <module>. Python is no different. The bulk of the functionality available in python resides in it’s standard libraries. Here is a visual that shows it.
For example, think of a program to calculate the area of a circle. You might already know the formula for the same. If not it is
Area of circle = Π x r2
Where Π is a constant and r is the radius of the circle. The math library in python provides the functionality of squaring numbers and giving the constant value for Π. Think of the library as a a plug-in or a bolt-on – except that is is provided by any standard python installation – and hence the name Python Standard library.
import math radius = input ("Enter the radius of the circle - " ) area = math.pi * math.pow(float(radius) , 2 ) print ( "Area of the circle is ", area)
There are a couple of things we have to notice here.
If you want to access the functionality in a module or library, you use the import function. There are a couple of variations to this, but essentially an import statement will do the job just fine. The import statement does a bunch of things some among which are
- Search for the module on the file system.
- If found, bind it to the local namespace.
When we refer to the value of pi ( Π ), we are using math.pi . Essentially the module math contains the constant of Π in the variable pi and we can’t just refer to it as pi. Instead we have to use the namespace math.pi . The indirection math is used to refer to the variable pi in the module math. You can have a local variable right here in the file pi and it would still be ok and not result in a clash. That is the beauty of namespace.
There are a couple of variations to the import syntax. Instead of importing all of the stuff in the math library, you can very well import specific functionality. For example, here, we are importing just the pi and pow ( power ) function and it is bound to the local namespace.
from math import pi, pow radius = input ("Enter the radius of the circle - " ) area = pi * pow(float(radius) , 2 ) print ( "Area of the cirlce is ", area)
And that is the reason why you don’t have to refer to pi as math.pi – instead you just use pi as though it is a local variable. Both work equally OK – neither version is optimized over the other. If you have long package names and you use it repeatedly enough in the program, you better go forfrom module import element1, element2 .. version. If not, just import the entire module. In the next set of chapters, we will see more examples of python standard libraries.
3rd Party Libraries
While the standard libraries are provided as part of the default python installation itself, 3rd party libraries are developed by somebody else and made available for download using a standard interface. In Python installation, we have seen example of using pip to install packages. Anytime you use pip or conda to install packages, think of them as 3rd party libraries or packages or modules.
The following visual would make you understand where 3rd party libraries stand in the context of core python and its standard libraries