This part of the documentation covers the installation of Requests.
The first step to using any software package is getting it properly installed.
$ python -m pip install requests¶
To install Requests, simply run this simple command in your terminal of choice:
$ python -m pip install requests
Get the Source Code¶
Requests is actively developed on GitHub, where the code is
always available.
You can either clone the public repository:
$ git clone https://github.com/psf/requests.git
Or, download the tarball:
$ curl -OL https://github.com/psf/requests/tarball/main # optionally, zipball is also available (for Windows users).
Once you have a copy of the source, you can embed it in your own Python
package, or install it into your site-packages easily:
$ cd requests $ python -m pip install .
Requests is an elegant and simple HTTP library for Python, built for human beings. One of the most famous libraries for python used by developers al over the world. This article revolves around how one can install requests library of python in Windows/ Linux/ macOS, etc.
Installation
Windows
For installing requests in windows, one would require Python (preferably latest version), so if you don’t have python installed, head to – How to download and install Python Latest Version on Windows. Now open command prompt from the windows and run following command –
python -m pip install requests
Booom..!! Done Now, requests library is downloaded successfully.
Linux
For installing requests in linux, one would require Python (preferably latest version) and pip latest version, so if you don’t have python installed, head to – How to download and install Python Latest Version on Linux. To install pip in linux – How to install PIP in Linux?. Now run,
pip install requests
macOS
For installing requests in mac, one would require Python (preferably latest version) and pip latest version, so if you don’t have python installed, head to – How to download and install Python Latest Version on mac. To install pip mac Os. Run,
sudo easy_install pip sudo pip install --upgrade pip
Now to install requests,
pip install requests
Alternative common method
THe last method for installation of requests on any operating system is to grab the base files and install requests manually and Requests is actively developed on GitHub, where the code is always available. For code – visit here.
You can either clone the public repository:
git clone git://github.com/psf/requests.git
Or, download the tarball:
curl -OL https://github.com/psf/requests/tarball/master # optionally, zipball is also available (for Windows users).
Once you have a copy of the source, you can embed it in your own Python package, or install it into your site-packages easily:
cd requests pip install .
For documentation of requests library – visit here
Last Updated :
06 Oct, 2021
Like Article
Save Article
Requests is a simple, yet elegant, HTTP library.
>>> import requests >>> r = requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass')) >>> r.status_code 200 >>> r.headers['content-type'] 'application/json; charset=utf8' >>> r.encoding 'utf-8' >>> r.text '{"authenticated": true, ...' >>> r.json() {'authenticated': True, ...}
Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your PUT
& POST
data — but nowadays, just use the json
method!
Requests is one of the most downloaded Python packages today, pulling in around 30M downloads / week
— according to GitHub, Requests is currently depended upon by 1,000,000+
repositories. You may certainly put your trust in this code.
Installing Requests and Supported Versions
Requests is available on PyPI:
$ python -m pip install requests
Requests officially supports Python 3.7+.
Supported Features & Best–Practices
Requests is ready for the demands of building robust and reliable HTTP–speaking applications, for the needs of today.
- Keep-Alive & Connection Pooling
- International Domains and URLs
- Sessions with Cookie Persistence
- Browser-style TLS/SSL Verification
- Basic & Digest Authentication
- Familiar
dict
–like Cookies - Automatic Content Decompression and Decoding
- Multi-part File Uploads
- SOCKS Proxy Support
- Connection Timeouts
- Streaming Downloads
- Automatic honoring of
.netrc
- Chunked HTTP Requests
API Reference and User Guide available on Read the Docs
Cloning the repository
When cloning the Requests repository, you may need to add the -c fetch.fsck.badTimezone=ignore
flag to avoid an error about a bad commit (see
this issue for more background):
git clone -c fetch.fsck.badTimezone=ignore https://github.com/psf/requests.git
You can also apply this setting to your global Git config:
git config --global fetch.fsck.badTimezone ignore
In this tutorial, you’ll learn how to install the popular requests package in Python, including on Windows, macOS, and Linux. The requests
library is a popular HTTP library that can handle generating different types of requests, including GET
, POST
, and PUT
requests. The library is available for Python 3 from the Python Package Index (PyPI).
By the end of this tutorial, you’ll have learned:
- How to install the requests library in Python for Windows, macOS, and Linux using the
pip
package manager - How to install the
requests
library using a Virtual Environment and a requirements.txt file - How to install
requests
from Github
Table of Contents
What is the Python requests Library?
The Python requests
library is a Python library that handles making HTTP requests. The library is well known for being simple and elegant, by abstracting away much of the complexity of working with HTTP requests. The library is also one of the more popular libraries available in Python: it currently draws around 30,000,000 downloads per week and is in use by over 1,000,000 repositories on Github.
How to Install requests on Windows Using pip
The simplest way to install the requests
library on Windows is to use the Python pip
package manager. In order to install the latest version of the library, you can simply call the following command in the command prompt:
python -m pip install requests
To install a specific version of the library, such as version 2.28.1, you can write the following command:
python -m pip install requests==2.28.1
It’s as easy as that! In the following section, you’ll learn how to install the requests
library on macOS using the pip
package manager.
How to Install requests on macOS Using pip
Similar to the Windows method, the simplest way to install the requests
library on macOS is by using the pip
package manager. On macOS, this is done by using the Terminal application. When in the Terminal application, simply run the following command:
pip install requests
Similar to installing a specific version on Windows, to install a specific version of the library, such as version 2.28.1, you can write the following command:
pip install requests==2.28.1
In the following section, you’ll learn how to install the requests
library on Linux.
How to Install requests on Linux Using pip
To install the requests
library using the pip
package manager on Linux, you can use the terminal application. When the application is open, you can run the following command:
pip install requests
Similar to the above example, to install a specific version of the library, such as version 2.28.1, you can write the following command:
pip install requests==2.28.1
In the following section, you’ll learn how to install the requests
library in a virtual environment.
How to Install requests in a Virtual Environment
Using a virtual environment is a good idea for many reasons. For one, it allows you to better understand what versions of libraries you’re using. Additionally, it allows you to keep a cleaner development environment.
Installing the requests
library in a virtual environment works the same as the methods above, though we first have to create and activate the virtual environment. You can create and activate the environment on Windows using the method below:
python -m venv venv
.\venv\Scripts\activate
On macOS, you can write the following:
virtualenv venv
source venv/bin/activate
Once the environment has been created, you can use any of the pip
methods shown above to install the requests
library. This is summarized in the code block below:
# On Windows:
python -m pip install requests
# On macOS or Linux
pip install requests
In the following section, you’ll learn how to install the library using a requirements.txt file.
How to Install requests With requirements.txt
Using a requirements.txt file is particularly helpful when sharing your code with others via source code management tools, such as Github. The file provides the ability to easily track and identify the packages that you use in a project.
In order to use the requirements.txt
file to install the requests
library, you can insert a file name requirements.txt
to the root folder of your project. In the file, include a line containing requests
.
From there, you can use the pip
package manager to install all libraries listed in the file. This can be done using the following command:
pip install -r requirements.txt
In the final section below, you’ll learn how to install the requests
library directly from Github source code.
How to Install requests from Github
If you have Git installed, you can install the requests
library directly from the source code. This allows you to install the library from its code directly.
In order to do that, you can use the pip
package manager, though you pass in the URL to the source code directly.
pip install github.com/kennethreitz/requests.git
Doing this can help you feel confident that the code you’re installing is the code you want to use.
Conclusion
In this tutorial, you learned how to install the requests
library on Windows, macOS, and Linux. You first learned how to install the library using the pip
package manager. Then, you learned how to install the library in a virtual environment. Finally, you learned how to install the library using a requirements.txt file as well as directly from Github.
Additional Resources
To learn more about related topics, check out the tutorials below:
- Python requests: GET Request Explained
- Python requests: POST Request Explained
- Python requests Response Object Explained
Python web development is preferable for many developers even tough if it is one of the oldest yet leading programming languages. Python thrives to keep up with the latest technological advancements along with its desirable features. One such great feature is its substantial number of easily available libraries. All you need to do is install the libraries, and start importing them at the beginning of your code. Moreover, you can install any number of libraries to your code according to your need.
Installing a library is not that complex as it sounds. So, here is a descriptive tutorial on how to install a requests library in Python.
What is the request library?
A Request library in Python handles the HTTP calls, where it simplifies the way to send the data on HTTP request and handles the same back in response. It eases the ways to carry out the CRUD operation and other HTTP call activities, such as data scraping. There is no restriction with the data it carries, it can carry everything from header to form data, query parameters, multipart files and others.
Request Library – Installation:
Firstly, before starting with the installation process, make sure that Python and pip were installed prior to ‘request’ module installation. To install the ‘requests’ library for Python, we can use any one of the following methods:
Here is the installation procedure for different operating systems.
Linux
$ pip install requests
In Linux, If you require root permission, use ‘sudo’. Alternatively, you can also use pipenv to install requests library, where pipenv is used to automatically manage the packages during the course of installation/uninstallation.
$ pip install pipenv
Windows
The Windows users need to navigate to the Python directory, and then install the request module as follows:
> python -m pip install requests
Mac
For MacOS, install Python through ‘Home Brew’. Thereafter, install pip and request module (which is the same as Linux installation process.)
Usage of Request Library
Once the installation part is over, you can execute the following functions with the help of request library.
1.Verify Python Installation
To verify that Python installed on the machine, try the below one:
$ python --version
If the version of Python is not returned, refer Python installation process and proceed with ‘request’ module installation process. For Python 3x, use the command python3 rather python.
$ python3 --version
We are using Python 2x in this blog for experimenting the request module installation. So, you can see this python keyword being in use throughout the article.
2. Access to Python Over Terminal
To access Python over command line, simply type python and hit enter to explore the request module (which is priorly installed)
$ python
3. Import Requests Library
Soon after the entry to the python command line, the appropriate module can be accessed through ‘import’ keyword. In our example, we import the request module for experimenting the functionality of the request module.
>>> import requests
4. To Send Request
A list of Http methods like GET, POST, PUT, PATCH, DELETE are accessible through the request module.
In the following example, a simple get request is sent and the corresponding response is recorded as follows:
>>> requests.get('https://www.example.com/')
The above request will return <Response [200]>, if successful.
5. To Parse Response
A Http status code for a request can be accessed through request module key status_code like 200, 201, 301, 404, 500, 501 etc.,
>>> response = requests.get('https://www.example.com/') >>> response.status_code
In the above one, a successful response of 200 returned.
Further, response data of the respective request can be parsed in a variety of formats such as content, text or JSON formats.
>>> response.content
This retrieves the content of the web page requested.
>>> response.text
The above code retrieves the response as string based on header content type, explicitly custom encoding can be passed by response.encoding = ‘utf-8’ or whichever the encoding scheme is required.
>>> response.json()
Here, it returns JSON for a valid response.
Other Frequently Used Http Methods:
Examples of widely used Http methods using request module in Python is as follows:
>>> requests.get('https://httpbin.org/get') >>> requests.post('https://httpbin.org/post', json={'python': {'library': 'requests'}}) >>> requests.put('https://httpbin.org/put', json={'key':'value'}) >>> requests.patch('https://httpbin.org/patch', json={'key':'value'}) >>> requests.delete('https://httpbin.org/delete') >>> cmd + d (to exit)
A key json carries the payload/body of Http call.
Here is the sample code for a little more clarity
This code sample covers quite a few use cases of request module.
# Importing Request module, which was installed prior to import requests # Http Methods: response = requests.get('https://httpbin.org/get') print (response.status_code) response = requests.post('https://httpbin.org/post', json={'python': {'library': 'requests'}}) print (response.request.body) response = requests.put('https://httpbin.org/put', json={'python': {'version': '2x', 'library': 'requests'}}) print (response.request.body) response = requests.patch('https://httpbin.org/patch', json={'python': {'version': '3x', 'library': 'requests'}}) print (response.request.body) response = requests.delete('https://httpbin.org/delete') print (response.status_code) # Status Codes: for url in ['https://httpbin.org/status/200', 'https://httpbin.org/status/400', 'https://httpbin.org/status/500']: try: response = requests.get(url) response.raise_for_status() # Raise exception, if applicable except: print ('Error with status code: ' + response.status_code) else: print ('Request completed successfully')
Just copy the sample code to “file-name.py”, then open the terminal and navigate to the directory where “file-name.py” resides. Now, run the following command.
$ python file-name.py
Therefore, a successful execution of code would result in use case output.
Hire best Python developers for your web development projects easily with Agira technologies. We provide you with dedicated experts who have mastered the latest technologies. Endure the fullest of outsourcing tech experts for your company. Get in touch with our experts to hire freelance python developers, offshore python developers and python web developers who can cater to your company’s requirement.
Read more blogs related to python web development. Such as Python Vs Node.js: What To Choose & Why – A Detailed Comparison and Developing RESTful APIs with Python and Django.
Ошибка: Контактная форма не найдена.