Skip to content

Requests - Environment Setup

Requests: Setting Up Your Python Environment

Section titled “Requests: Setting Up Your Python Environment”

Before you can start making HTTP requests with the requests library, you need to have Python installed and then install the requests package itself. This chapter guides you through the setup process, emphasizing modern best practices like using virtual environments.

  • Ensuring Python is Installed
  • Understanding and Using Virtual Environments
  • Installing the requests Library

The requests library is a Python package, so Python is a prerequisite. Most modern operating systems (Linux and macOS) come with Python pre-installed. For Windows, or if you need a specific version, you’ll need to install it.

Visit the official Python website at https://www.python.org/downloads/. Download the latest stable version suitable for your operating system (Windows, macOS, Linux/UNIX).

During installation (especially on Windows), ensure you check the box that says something like “Add Python to PATH” or “Add python.exe to Path”. This makes Python and its package manager, pip, accessible from your command line or terminal.

Open your terminal or command prompt and type the following commands to check if Python and pip are correctly installed and configured in your PATH:

python --version
# or on some systems
python3 --version

This should print the installed Python version (e.g., Python 3.11.4).

pip --version
# or
pip3 --version

This should print the pip version and its location.

If these commands don’t work, you might need to troubleshoot your Python installation or PATH configuration. Refer to Python’s official documentation for help.

2. Understanding and Using Virtual Environments

Section titled “2. Understanding and Using Virtual Environments”

Before installing requests or any other Python package, it’s a strong best practice to create a virtual environment. A virtual environment is an isolated Python setup that allows you to manage dependencies for a specific project separately, preventing conflicts between projects that might require different versions of the same library.

Python 3 comes with the venv module for creating virtual environments. Navigate to your project directory in your terminal and run:

# Replace 'myproject_env' with your desired environment name
python -m venv myproject_env
# or on some systems
python3 -m venv myproject_env

This command creates a new folder (e.g., myproject_env) containing a fresh Python installation and pip specific to this environment.

Before you can use the virtual environment, you need to activate it:

On Windows (Command Prompt/PowerShell):

.\myproject_env\Scripts\activate

On macOS and Linux (bash/zsh):

source myproject_env/bin/activate

Once activated, your terminal prompt will usually change to indicate the active environment (e.g., (myproject_env) Your-Computer:your_project user$). Now, any packages you install using pip will be installed within this isolated environment.

With your virtual environment activated, you can now install the requests library using pip:

pip install requests

pip will download requests and its dependencies (like certifi, charset-normalizer, idna, urllib3) from the Python Package Index (PyPI) and install them into your active virtual environment.

You can verify that requests was installed correctly by checking its version with pip show:

pip show requests
Name: requests
Version: 2.31.0 # Version may vary
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /path/to/your/project/myproject_env/lib/pythonX.Y/site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by:

You can also try importing it in a Python interpreter:

python
>>> import requests
>>> print(requests.__version__)
# Expected output: version string like '2.31.0'

When you’re done working on your project, you can deactivate the virtual environment by simply typing:

deactivate

This command is the same for all operating systems.

Managing Dependencies with requirements.txt

Section titled “Managing Dependencies with requirements.txt”

As you add more packages to your project, it’s good practice to keep track of them in a requirements.txt file. You can generate this file with:

pip freeze > requirements.txt

This file can then be used by others (or your future self) to easily install all the necessary dependencies for the project using pip install -r requirements.txt.

You are now all set to start using the requests library in your Python projects!