Skip to content

How to fix the MissingSchema error in Python requests

A MissingSchema error means the URL you passed to requests is not complete enough for the library to know how to reach it.

OUTPUT FORMATSHTMLJSONMarkdown

The two common causes are a missing scheme (http:// or https://) and a relative path such as /path/to/resource. This guide shows how to confirm which one you have and how to repair the URL before the request is made.

What triggers MissingSchema

requests needs an absolute URL with a scheme. If the string starts with a bare hostname, or with a path fragment like /path/to/resource, the library cannot build a request target and raises MissingSchema. Before changing code, print the URL you are about to pass and check two things: does it begin with http:// or https://, and is it a full address rather than a path relative to some other page? A URL that fails either check is the likely culprit.

Repairing a relative URL with urljoin

When the URL is a relative path, combine it with a known base URL using urljoin from urllib.parse. The pattern is: import urljoin, define the base URL, define the relative path, call urljoin(base, relative), then pass the result to requests.get. The base must include the scheme, because urljoin uses it to build the final absolute URL. The exact reference implementation is supplied in the accompanying protected code module and should be used as the canonical pattern.

urljoin only merges relative paths

A useful caveat: urljoin merges the two arguments only when the second is a relative path. If the second argument is itself an absolute URL, it replaces the base rather than being appended to it. That behaviour is correct for URL resolution but can surprise callers who expect concatenation. The supplied second code module demonstrates this with two absolute URLs and shows the printed result. If your intent is to append a path, make sure the second argument is genuinely relative.

Checklist before you retry

  1. Print the exact URL string passed to requests. 2. Confirm it starts with http:// or https://. 3. If it is a path, join it to a base URL that includes the scheme. 4. If you use urljoin, confirm the second argument is relative, not absolute. 5. Only after those checks, retry the request. If the error persists with a full absolute URL, the problem is likely a different exception class, such as InvalidSchema, and should be diagnosed separately.

Frequently asked questions

Does MissingSchema mean the server is unreachable?

No. MissingSchema is raised before any network call because the URL string is incomplete. The server is not contacted, so reachability is not the issue.

Can I fix MissingSchema without urljoin?

Yes. If you already know the full absolute URL, pass it directly with the scheme included. urljoin is useful when you have a base URL plus a relative path and want to combine them reliably.

Why does urljoin ignore my base URL?

Because the second argument is an absolute URL. urljoin only merges when the second argument is a relative path; an absolute second argument replaces the base.