How to install requests python
How to install requests python
How to install requests in Python – For windows, linux, mac
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 –
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,
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,
Now to 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:
Or, download the tarball:
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:
For documentation of requests library – visit here
How to install requests module in Python 3.4, instead of 2.7
I have both Python 2.7 and 3.4 installed on my Ubuntu 14.04 machine. I want to install the ‘requests’ module so it is accessible from Py3.4.
When I issued pip install requests on my terminal cmd line I got back:
How can I direct pip to install requests for 3.4 even though it is already in 2.7?
6 Answers 6
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Python 3.4 has pip support built-in, so you can also use:
If you’re running Ubuntu (or probably Debian as well), you’ll need to install the system pip3 separately:
On Windows with Python v3.6.5
Just answering this old thread can be installed without pip On windows or Linux:
1) Download Requests from https://github.com/kennethreitz/requests click on clone or download button
3) Depending on the Os run the following command:
while installing python packages in a global environment is doable, it is a best practice to isolate the environment between projects (creating virtual environments). Otherwise, confusion between Python versions will arise, just like your problem.
The simplest method is to use venv library in the project directory:
Where the first venv is to call the venv package, and the second venv defines the virtual environment directory name. Then activate the virtual environment:
Another benefit of the virtual environment is to have a concise list of libraries needed for that specific project.
*note: commands only work on Linux and Mac OS
psf/requests
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Requests is a simple, yet elegant, HTTP library.
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:
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.
API Reference and User Guide available on Read the Docs
Cloning the repository
You can also apply this setting to your global Git config:
How To Get Started With the Requests Library in Python
Introduction
In many web apps, it’s normal to connect to various third-party services by using APIs. When you use these APIs you can get access to data like weather information, sports scores, movie listings, tweets, search engine results, and pictures. You can also use APIs to add functionality to your app. Examples of these are payments, scheduling, emails, translations, maps, and file transfers. If you were to create any of those on your own it would take a lot of time, but with APIs, it can take only minutes to connect to one and access its features and data.
In this article, you will learn about the Python Requests library, which allows you to send HTTP requests in Python.
Warning: The second half of this tutorial relied upon free API keys for Yandex.Translate. At this point in time, these are no longer being issued freely. The rest of this tutorial is presented as-is for educational purposes.
And since using an API is sending HTTP requests and receiving responses, Requests allows you to use APIs in Python. We’ll demonstrate the use of a language translation API here so you can see an example of how it works.
Prerequisites
To complete this tutorial, you will need:
Understanding HTTP Requests
HTTP requests are how the web works. Every time you navigate to a web page, your browser makes multiple requests to the web page’s server. The server then responds with all the data necessary to render the page, and your browser then actually renders the page so you can see it.
The generic process is this:
Part of the data the client sends in a request is the request method. Some common request methods are GET, POST, and PUT. GET requests are normally for reading data only without making a change to something, while POST and PUT requests generally are for modifying data on the server. So for example, the Stripe API allows you to use POST requests to create a new charge so a user can purchase something from your app.
Note: This article will cover GET requests because we won’t be modifying any data on a server.
When sending a request from a Python script or inside a web app, you, the developer, get to decide what gets sent in each request and what to do with the response. So let’s explore that by first sending a request to DigitalOcean.com and then by using a language translation API.
Step 1 — Installing Python Requests
It is a good idea to create a virtual environment first if you don’t already have one.
At this point, your project is ready to use Requests.
Step 2 — Making Your First Request
To start, let’s use Requests for requesting the DigitalOcean site.
Create a file called script.py and add the following code to it. In this article, we won’t have much code to work with, so when something changes you can just update the existing code instead of adding new lines.
So all this code is doing is sending a GET request to DigitalOcean. This is the same type of request your browser sent to view this page, but the only difference is that Requests can’t actually render the HTML, so instead you will just get the raw HTML and the other response information.
You can run it by executing the script.py file.
And here’s what you get in return:
Next, let’s investigate what a 200 status code means.
Step 3 — Understanding Status Codes
The first thing you can do is check the status code. HTTP codes range from 1XX to 5XX. Common status codes that you have probably seen are 200, 404, and 500.
Here’s a quick overview of what each status code means:
Generally, what you’re looking for when you perform your own requests are status codes in the 200s.
You can test if a request responded successfully by checking the response for truth. For example:
And here’s what you get in return:
The message «Response Failed» will only appear if a 400 or 500 status code returns. Try changing the URL to some nonsense to see the response fail with a 404.
You can take a look at the status code directly by adding:
This will show you the status code directly so you can check the number yourself.
This displays the response, the script message, and the response status code.
Step 4 — Understanding Headers
Another thing you can get from the response is the headers. You can take a look at them by using the headers dictionary on the response object.
This produces the following output:
Headers are sent along with the request and returned in the response. Headers are used so both the client and the server know how to interpret the data that is being sent and received in the response.
You can see the various headers that are returned. A lot of times you won’t need to use the header information directly, but it’s there if you need it.
The content type is usually the one you may need because it reveals the format of the data (HTML, JSON, PDF, text, etc.). But the content type is normally handled by Requests so you can access the data that gets returned.
Step 5 — Understanding Response Text
And finally, if you take a look at res.text (this works for textual data, like the HTML page you are currently viewing) you can see all the HTML needed to build the home page of DigitalOcean. It won’t be rendered, but you see that it looks like it belongs to DigitalOcean.
This produces the following output:
If you saved this to a file and opened it, you would see something that resembled the DigitalOcean site. In a real situation, multiple requests are made for a single web page to load things like images, scripts, and stylesheets, so if you save only the HTML to a file, it won’t look anything like what the DigitalOcean site looks like in your browser because only a single request was performed to get the HTML data.
Step 6 — Using the Translate API
So now let’s move on to something more interesting. You’ll use the Yandex Translate API to perform a request to translate some text to a different language.
To use the API, first, you need to sign up. After you sign up, go to the Translate API and create an API key. Once you have the API key, add it to your file as a constant. Here’s the link where you can do all those things: https://tech.yandex.com/translate/
The reason why you need an API key is so Yandex can authenticate us every time you want to use their API. The API key is a lightweight form of authentication because it’s added to the end of the request URL when being sent.
To know which URL you need to send to use the API, you can look at the documentation for Yandex.
If you look there, you’ll see all the information needed to use their Translate API to translate text.
So let’s add some code to send to that URL. You can replace the first request you created with this:
There are two ways you can add the parameters. You can either append it to the end of the URL directly, or you can have Requests do it for you. To accomplish the latter, you can create a dictionary for our parameters. The three items you need are the key, the text, and the language. Let’s create the dictionary using the API key, ‘Hello’ for the text, and ‘en-es’ as the lang, which means you want to translate from English to Spanish.
If you need to know any other language codes, you can look at this list of ISO 639-1 codes. Focus on the 639-1 column.
You can create a params dictionary by using the dict() function and passing in the keys and values you want in your dictionary.
When you pass the parameters this way, Requests will go ahead and add the parameters to the URL for you.
Now, let’s add a print statement for the response text and view what gets returned in the response.
This produces the following output:
You will see three things. You will see the status code, which is exactly the same status code of the response itself, you will see the language that you specified, and you will see the translated text inside of the list. So you should see ‘Hola’ for the translated text.
Try again with en-fr as the language code, and you should see ‘Bonjour’ in the response now.
This produces the following output:
Let’s take a look at the headers for this particular response.
This produces the following output:
When application/json is the content type of the response, you are able to have Requests convert the response to a dictionary and list so you can access the data easier.
If you print it, you’ll see that the data looks the same, but the format is slightly different.
This produces the following output:
Let’s say you want to access the text. Since this is now a dictionary, you can use the text key.
This produces the following output:
And now you only see the data for that one key. In this case, you are looking at a list of one item, so if you wanted to get that text in the list directly, you could access it by the index.
This produces the following output:
And now the only thing you see is the translated word.
This produces the following output:
Try translating longer text in different languages and see what responses the API gives you.
Step 7 — Handling Translate API Error Cases
Finally, we’ll take a look at an error case. Everything doesn’t always work, so you need to know when that happens.
Try changing your API key by removing one character. When you do this your API key will no longer be valid. Then try sending a request.
If you take a look at the status code, this is what you get:
This produces the following output:
When you are using the API, you’ll want to check if things are successful or not so you can handle the error cases according to the needs of your app.
Conclusion
In this article, you learned about the Python Requests library, which allows you to send HTTP requests in Python.
Here’s what you learned:
If you’d like to learn more about Python, check out our React topic page for exercises and programming projects.
Want to learn more? Join the DigitalOcean Community!
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
How to Install requests in Python?
The Python requests library is among the top 100 Python libraries, with more than 118,712,094 downloads. This article will show you everything you need to get this installed in your Python environment.
Table of Contents
How to Install requests on Windows?
Here’s how to open the command line on a (German) Windows machine:
First, try the following command to install requests on your system:
Second, if this leads to an error message, try this command to install requests on your system:
Third, if both do not work, use the following long-form command:
How to Install requests on Linux?
You can install requests on Linux in four steps:
The package is now installed on your Linux operating system.
How to Install requests on macOS?
Similarly, you can install requests on macOS in four steps:
The package is now installed on your macOS.
How to Install requests in PyCharm?
Given a PyCharm project. How to install the requests library in your project within a virtual environment or globally? Here’s a solution that always works:
Here’s the general package installation process as a short animated video—it works analogously for requests if you type in “requests” in the search field instead:
Make sure to select only “requests” because there may be other packages that are not required but also contain the same term (false positives):
How to Install requests in a Jupyter Notebook?
This automatically installs the requests library when the cell is first executed.
How to Resolve ModuleNotFoundError: No module named ‘requests’?
Say you try to import the requests package into your Python script without installing it first:
To fix the error, install the requests library using “ pip install requests ” or “ pip3 install requests ” in your operating system’s shell or terminal first.
See above for the different ways to install requests in your environment.
Improve Your Python Skills
If you want to keep improving your Python skills and learn about new and exciting technologies such as Blockchain development, machine learning, and data science, check out the Finxter free email academy with cheat sheets, regular tutorials, and programming puzzles.
Join us, it’s fun! 🙂
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.