How to install a library in Python
Python is a versatile and powerful programming language with an extensive ecosystem of libraries that help developers build everything from web applications to data analysis tools. These libraries contain ready-made code that you can use in your projects so you don’t have to reinvent the wheel. To use these libraries, you need to install them first. Python can be used in a wide range of hosted services for various purposes such as web hosting, automation, machine learning, and serverless applications.
Step 1: Install Python
Before installing the libraries, you need to make sure that Python is installed on your system.
To check if Python is installed, follow the steps below:
- Open a terminal (on Linux or macOS) or a command prompt (on Windows).
- Type the following command and press Enter:
python --version
If Python is installed, it will return the installed version number (for example, Python 3.9.5).
If Python is not installed:
- On Windows, download and install Python from the official website: https://www.python.org/downloads/. During installation, be sure to check the “Add Python to PATH” checkbox.
- On Linux or macOS, Python may already be installed by default. If not, you can install it using your system’s package manager (e.g. sudo apt install python3 on Ubuntu).
Step 2: Install pip (the Python package installer)
pip is the default package installer for Python. It allows you to easily install libraries and manage dependencies. In most cases pip is already installed with Python, but you can check it.
To check if pip is installed:
- Open a terminal or command prompt.
- Type the following command:
pip --version
If pip is installed, it will display the version number (e.g. pip 21.1.1.1 from …).
If pip is not installed:
- You can install it by downloading the get-pip.py script and running it:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py
Once installed, pip should be available for use.
Step 3: Installing the Python library using pip
Once Python and pip are set up, you can install any Python library from the Python Package Index (PyPI), which hosts thousands of libraries.
To install a library using pip:
- Open a terminal or command prompt.
- Use the following command to install the library:
pip install library_name
For example, to install the popular query library, type:
pip install requests
pip will download the package and all the dependencies required for it, and then install it on your system.
Step 4: Verify the library installation
To verify that the library has been installed correctly, you can import it into Python scripts or the interactive Python shell.
To verify the installation:
- Open the interactive Python shell by typing python at the terminal or command line.
- Try importing the installed library:
import requests
If the library is imported without errors, the installation was successful.
Step 5: Install specific versions of the library
Sometimes you may need to install a specific version of a library for compatibility reasons.
To install a specific version:
Use the following syntax:
pip install library_name==version_number
For example, to install version 2.25.1 of the requests library:
pip install requests==2.25.1
pip will install the specified version and all necessary dependencies.
Step 6: Update an existing library
To upgrade a library to the latest version, you can use the –upgrade flag.
To upgrade a library:
pip install --upgrade library_name
For example, to update the requests library:
pip install --upgrade requests
Step 7: Installing libraries from the requirements file
In large projects, you may need to install several libraries at once. Developers often use a requirements.txt file that lists all the dependencies needed for a project.
To install libraries from the requirements.txt file, follow these steps:
- Create a text file named requirements.txt and list each library and its version on a new line. For example:
requests==2.25.1 numpy==1.20.3 pandas==1.2.4
- Use pip to install all the libraries listed in the file:
pip install -r requirements.txt
pip will read the file and install all the specified libraries along with their correct versions.
Step 8: Using virtual environments to manage libraries
It is common for Python projects to use virtual environments to isolate dependencies and ensure that different projects do not conflict with each other.
To create a virtual environment:
- First, install the virtual environment package:
pip install virtualenv
- Create a virtual environment in the project folder:
virtualenv venv
- Activate the virtual environment:
- In Windows:
venv\Scripts\activate
- In Linux/macOS:
venv/bin/activate source
- In Windows:
- When the virtual environment is activated, install the libraries as usual:
pip install library_name
The libraries will only be installed in the virtual environment, keeping your global Python installation clean.
To deactivate the virtual environment:
deactivate
Step 9: Deleting the library
If you need to remove a library, pip also allows you to uninstall it.
To uninstall a library:
pip uninstall library_name
For example, to remove the library requests:
pip uninstall requests
pip will remove the package from your system.
Conclusion
Installing Python libraries is a simple but powerful process that allows you to extend the functionality of your projects. With tools such as pip and virtual environments, you can manage libraries efficiently and ensure that your projects have all the dependencies you need. Whether you are installing a single library or managing multiple dependencies, the steps outlined in this article will guide you through the process with ease.