5. Install requests and an editor

Two tools to install and you're nearly done. The requests library is what you'll use to make API calls in every chapter from here on. VS Code is the editor that integrates cleanly with virtual environments and Python.

Python's built-in tools for making HTTP requests are verbose and difficult to use. The requests library simplifies everything, making API calls feel natural and intuitive. It's so popular that it's considered the de facto standard for HTTP in Python.

Compare these two approaches to making the same API call:

verbose_request.py (avoid)
import urllib.request
import json

url = "https://api.example.com/data"
request = urllib.request.Request(url)
request.add_header('Content-Type', 'application/json')

try:
    response = urllib.request.urlopen(request)
    data = json.loads(response.read().decode('utf-8'))
except Exception as e:
    print(f"Error: {e}")
clean_request.py (preferred)
import requests

url = "https://api.example.com/data"
response = requests.get(url, timeout=10)
data = response.json()

The requests library reduces 10+ lines of boilerplate to 4 readable lines. Common request details like headers, encoding, and JSON parsing become much easier to handle. This is why professional Python developers usually reach for requests.

Installing requests

Before installing: Make sure your virtual environment is activated. Look for the (my-first-api-env) prefix in your terminal. If you don't see it, activate the environment now.

Terminal
python -m pip install requests
Output
Collecting requests
  Downloading requests-2.32.x-py3-none-any.whl
Collecting charset-normalizer<4,>=2
  Downloading charset_normalizer-3.x.x-py3-none-any.whl
Collecting idna<4,>=2.5
  Downloading idna-3.x-py3-none-any.whl
Collecting urllib3<3,>=1.21.1
  Downloading urllib3-2.x.x-py3-none-any.whl
Collecting certifi>=2017.4.17
  Downloading certifi-2026.x.x-py3-none-any.whl
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2026.x.x charset-normalizer-3.x.x idna-3.x requests-2.32.x urllib3-2.x.x

The installation downloads requests and its dependencies. This takes 10-30 seconds depending on your internet speed. Your exact version numbers may differ, but you'll see a "Successfully installed" message when it's done.

pip connected to the Python Package Index (PyPI), downloaded the requests library and all the packages it depends on, and installed everything into your active virtual environment. Because your environment was activated, these packages installed there instead of to your system Python.

Verifying the installation

Let's confirm requests is installed and working correctly:

With your virtual environment still active, type:

Terminal
python

Inside an activated virtual environment, python points to that environment's Python on Windows, macOS, and Linux. If you are not using an activated virtual environment, some macOS/Linux systems still require python3.

You'll see Python's interactive prompt: >>>

Terminal
>>> import requests
>>> requests.__version__
Output
'2.32.x'

If you see a version number, congratulations! requests is installed and ready to use. The exact version number doesn't matter as long as it's 2.20 or higher.

Exit the Python REPL so you can continue at your normal command prompt:

Python REPL
>>> exit()

This returns you to your normal command prompt, still with your virtual environment active.

If you see this error:

Error output
>>> import requests
ModuleNotFoundError: No module named 'requests'

This means requests isn't installed in your current Python environment. The most common cause is that pip and python are pointing at different environments. Exit the Python REPL with exit(), then go to Troubleshooting Issue 7.

Setting up your code editor

You need a place to write Python code. While you could technically use Notepad or TextEdit, a proper code editor makes programming dramatically easier. Visual Studio Code (VS Code) is the recommended editor: free, powerful, cross-platform, and with excellent Python support.

That said, if you already use another editor (PyCharm, Sublime Text, Vim, Emacs), stick with what you know. The key is having somewhere to write and save Python files with .py extensions.

Installing VS Code (recommended)

  1. Go to code.visualstudio.com
  2. Download the installer for your operating system
  3. Run the installer (accept all default settings)
  4. Launch VS Code

First launch takes a few extra seconds as VS Code sets itself up. Once it opens, you'll see a clean interface with a sidebar on the left and a main editor area.

Installing the Python extension

VS Code becomes significantly more powerful with the Python extension installed:

  1. Click the Extensions icon in the left sidebar (looks like four squares)
  2. Search for "Python"
  3. Find the extension by Microsoft (it's usually first)
  4. Click "Install"

This extension adds syntax highlighting, error detection, code completion, debugging, and automatic formatting. It's what transforms VS Code from a text editor into a Python IDE.

After opening your api-projects folder, VS Code may ask you to select a Python interpreter. Choose the interpreter inside my-first-api-env. If VS Code does not ask, open the Command Palette with Ctrl+Shift+P on Windows/Linux or Command+Shift+P on macOS, search for Python: Select Interpreter, and choose the path that includes my-first-api-env. This makes autocomplete, error checks, and the integrated terminal line up with the same virtual environment.

Creating and running your first file

Let's verify everything works by creating and running a simple Python file:

  1. Open your projects folder. In VS Code: File → Open Folder → Navigate to api-projects → Select Folder
  2. Create a new file. Click the "New File" icon or use File → New File. Save it as hello_python.py in your api-projects folder.
  3. Write test code.
hello_python.py
print("Hello from my API workspace!")

Run the file from your terminal with the virtual environment activated:

Terminal
python hello_python.py
Output
Hello from my API workspace!

If you see the message, everything works! You can write code in VS Code and run it from your terminal. This is the workflow you'll use throughout the book.

VS Code tips for beginners

  • Integrated terminal. View → Terminal opens a command line inside VS Code. This is often more convenient than switching between windows.
  • Auto-save. File → Auto Save enables automatic file saving. This prevents lost work.
  • Python interpreter. VS Code may ask you to select a Python interpreter. Choose the one inside your my-first-api-env folder.
  • Formatting. Right-click → Format Document makes code clean and consistent. Keyboard shortcut: Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS).

You don't need to master VS Code immediately. You'll learn features naturally as you write more code. For now, just being able to create files, write code, and save them is enough.

Next, you'll run one final verification script that checks Python, your virtual environment, requests, and a live test connection together.