AI & Python #36: Python Libraries You Should Learn
Multipurpose Python libraries that you’ll need regardless of your specialization.
Everyone has been in a situation where they don’t know what to learn next or what to specialize in.
You might like Python but that doesn’t mean you’ll learn every single library out there. You also don’t want to learn libraries that you may never use, so what should you do?
Well, there are a few Python libraries that will come in handy regardless of the field you’re in. Whether you’re into web development or data analysis, you’d need these libraries to some degree.
Here are some Python modules, libraries, and packages that you should learn whenever you don’t know what to learn next.
OS/Pathlib
Working with paths is something that anyone does sooner or later. Whether you’d like to export a dataset in a specific directory or read static files for your web application, you’d need to learn the OS or Pathlib modules.
The OS and Pathlib are Python modules that allow us to do file system operations such as creating directories, listing directory content, working with paths, and more.
But which one should you learn? OS or Pathlib?
The OS and Pathlib modules have some similarities. With the OS module, you can manage paths, create folders, and more, but this module represents paths as string values. This limitation may sometimes lead you to search for workarounds that could be avoided by using the Pathlib module.
Obtaining the parts of a path using Pathlib is as simple as calling an attribute. Let’s have a look.
from pathlib import Path
# obtaining the current working directory
my_path = Path.cwd()
>>> my_path
PosixPath('/Users/frankandrade')
>>> my_path.parent
PosixPath('/Users')
>>> my_path.name
'frankandrade'
Unlike OS, Pathlib works with objects that have properties and internal methods that make working with paths simpler.
Requests
Whenever you need to interact with a server, you’ll have to use the Requests library. This is a Python library that you can use to send HTTP requests. HTTP is a request-response protocol between a client and a server that is the foundation of data exchange on the web.
Why do you need that? Nowadays it’s very common to connect to third-party services using APIs, so we can get access to data such as tweets and the weather or add functionality to your web app like adding payment, translation, and email. If those APIs didn’t exist, you’d have to create those services from scratch!
Fortunately, with APIs, we only need some minutes to get access to such services. That said, if you want to work with an API, you’ll need to send HTTP requests and receive responses. As you might guess, the Requests library is the perfect tool for this.
Here’s an example of how I used the requests library to perform speech-to-text using this API.
import requests
...
headers = {'authorization': api_key,
'content-type': 'application/json'}
response = requests.post(upload_endpoint,
headers=headers,
data=read_file(filename))
audio_url = response.json()['upload_url']
But that’s not all! If you add a web scraping library like Beautiful Soup, you can even extract data from websites using requests.
Selenium
Every job has tasks that can be automated, and unless you want to waste time doing it manually, you should learn Selenium to automate most websites out there.
Selenium is an automation testing tool that you can use to automate websites with Python. With Selenium, you can perform web actions such as clicking on buttons, navigating through pages, selecting elements within a dropdown, and even extracting data!
You can also customize the bot further by adding waits and rotating proxies, so the bot behaves like a “regular person.”
Here’s some Python code to open a browser, go to a website, and then quit it using Selenium.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
web = 'https://www.example.com'
path = '/.../chromedriver'
# Creating the driver
driver_service = Service(executable_path=path)
driver = webdriver.Chrome(service=driver_service)
driver.get(web)
# Quit driver
driver.quit()
And here’s a demo.
The best thing about learning Selenium is that once you master it, you can automate not only one but most websites out there.
Here’s a video tutorial to get you started with Selenium in Python.
PyTest
Anyone learning Python should be able to prepare their code for an industry setting.
How to do that? Testing your code!
PyTest is a framework that allows us to write test codes in Python. With Pytest, we can write small, readable tests that can scale to support complex functional testing for apps and libraries.
But why do we need to test our code? Well, we can make little mistakes when writing code that aren’t easy to detect simply because they don’t throw an error or crash a program. Your code may run fine once, but it could give you some headaches in the long run. This is why we need to test our code to avoid unexpected results and ensure we can rely on our output.
There are different ways to test code, but Pytest makes testing simple and also allows us to see which tests failed and which succeeded.
Here’s a test I made:
If you know a library that can be added to this list, let me know in the comments 👇