How to import a library in Python?
In Python, libraries are used to refer to a collection of modules that are used repeatedly in various programs without the need of writing them from scratch.
Modules on the other hand refer to any Python file saved with the .py extension. Modules often contain code such as functions, classes and statements that can be imported and used within other programs.
In this article, we’ll show you how to import a library in Python step by step.
Besides built-in functions that we can use such as the print function, we can also use built-in modules to build more sophisticated programs.
How to import a library in Python – basics
We can also create our own modules that we can reuse in other programs. We can import and use modules across different programs using keyword import.
Modules in the Python standard library need no installing and therefore importing them at the top of our programs is enough to get started.
Modules provided in the standard library provide us with access to system functionality among other standard solutions.
A common example of such libraries is the math library. The math library provides access to common mathematical functions.
Using these functions we can perform various mathematical expressions such as finding the square root of a number.
Libraries not provided in the standard library need to be installed first. Once they have been installed in our system we can then proceed to import them into our programs.
There are a number of ways we can install various libraries, some of the common ones is the use of the pip package manager.
Although pip is installed by default in versions of Python later than 3.4, it can also be installed using the command:
pip install pip
Pip allows us to install packages from indexes such as the Python Package Index. One of the most commonly installed and used libraries in python is the requests library.
The request library is the defacto for making HTTP requests in Python. It is installed by running the command below.
pip install requests
If the request library is already installed in your system you’re likely to get the message that the requirement is already satisfied. Otherwise, the requests library will be installed automatically.
How to import a library in Python – importing specific items in a module
Using the keywords from…import we can import specific items from a library or module. These may include functions or classes within that specific library.
This is important especially when you do not intend to use only a specific function in a module and therefore it is needless to import the entire module.
For instance, in the example below instead of importing the entire random module, we are just importing the specific function that we can use to create a random integer within a certain range as shown below.
from random import randint
This is an alternative method to using the dot notation in accessing various elements in a module.
How to import a library in Python – importing modules as an alias
Python allows us to change the names of modules and functions and name them as we want. This is useful in scenarios where a module may have taken up a certain name and we want to use a different one.
It is also convenient when a module has a very long name and rewriting that name throughout the program is inconvenient and may cause some bugs due to misspelling.
The name given to the module or library in place of the original name is known as the alias. In the example below we are importing the random library as an alias.
Some libraries such as the pandas, NumPy libraries have de facto alias names. In such a case it is advisable that we stick to the common practice of aliasing these libraries as such.
import pandas as pd
import numpy as np
Summary
Those are the basic methods and modules that you need to know if you’d like to learn how to import a library in Python.
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
