The Requests library is a popular Python library used for making HTTP requests. It is an essential tool for web scraping and API interactions. Here's how you can install the Requests library in Python.
Installation using pip
The easiest method to install Requests is by using pip
, which is the package installer for Python. If you have Python installed, it's likely that you also have pip
. To install Requests, open your command line (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows) and run the following command:
pip install requests
If you are using Python 3 (which is highly recommended as Python 2 is no longer supported), you may need to use pip3
:
pip3 install requests
Installing inside a virtual environment
It's a good practice to use a virtual environment for your Python projects to manage dependencies separately for each project. To install Requests inside a virtual environment, first, you will need to create and activate the environment. Here's how to do that:
For macOS/Linux:
python3 -m venv myenv
source myenv/bin/activate
pip install requests
For Windows:
python -m venv myenv
myenv\Scripts\activate
pip install requests
Replace myenv
with whatever you want to name your environment. After activation, the command prompt will change to indicate that you are now inside a virtual environment, and you can proceed to install Requests using pip
.
Installing from source
If you need to install Requests from the source, for example, if you want the latest development version or a specific commit, you can do so by cloning the GitHub repository and installing it manually:
git clone https://github.com/psf/requests.git
cd requests
python setup.py install
Verifying the installation
After installation, you can verify that Requests is installed correctly by running the following command in your Python interpreter or script:
import requests
print(requests.__version__)
This should output the installed version number of the Requests library, confirming that it is installed and accessible in your Python environment.
Note on Python and pip versions
Depending on your system, python
and pip
commands might point to Python 2. It's important to make sure that you are using Python 3 and its associated pip version (pip3
), as Python 2 reached its end of life on January 1, 2020. If you're unsure, you can check the versions with python --version
and pip --version
.
That's all you need to install the Requests library in Python! With Requests installed, you can now proceed to use it for sending HTTP requests in your projects.