Coding

Online Python Interpreter: Run Python in Your Browser

An online Python interpreter lets you type Python in a web page and run it without installing anything. Some run your code in your own browser (usually through WebAssembly), and others send it to a server and show you the output.

Which kind you are using decides what the interpreter can do, how private your code is, and why some programs fail there that work fine on your computer. This guide explains the difference, lists the limits, and shows how to run Python in a page yourself.

The two ways an online Python interpreter works

Runs in your browserRuns on a server
Where your code executesOn your device, inside the browser sandboxOn the site's machine
Is your code uploaded?No, the page loads the runtime once and then runs locallyYes, every run sends it to the server
Typical technologyPyodide (CPython compiled to WebAssembly)A Python install on the site's machine
PackagesLimited to what runs in WebAssemblyDepends on the site
Networking and threadsRestricted (see the limits below)Depends on the site

You can't tell from the outside which kind a site is, so check its FAQ or privacy page. A reliable test: open the browser's developer tools, switch to the Network tab, run a snippet, and see whether a request carrying your code leaves the page. If nothing is sent, it ran locally.

How Python runs in a browser

Browsers can't run CPython directly. They can run WebAssembly, which MDN describes as "a low-level bytecode format originally designed for the web" and "an effective compilation target for source languages like C, C++, Rust". CPython is written in C, so it can be compiled to WebAssembly. Pyodide is that port: its documentation calls it a Python runtime that runs in WebAssembly and "may be used in a web browser or a backend JavaScript environment."

The security model comes from the browser. MDN states that WebAssembly "is specified to be run in a safe, sandboxed execution environment" that enforces the browser's same-origin and permissions policies like other web code. The Python code in a Pyodide page cannot read your files or open arbitrary programs, because the browser doesn't allow it.

Run Python in a web page yourself

You don't need an existing site. Pyodide's usage page describes three steps: add a script tag that loads pyodide.js from its CDN (the docs give the exact URL for the current release), call loadPyodide() inside an async function, then run code with runPython(). The JavaScript part looks like this:

async function main() {
  const pyodide = await loadPyodide();
  console.log(pyodide.runPython("1 + 2"));
}
main();

Example output in the browser console: 3. The value of the last expression is returned to JavaScript. To run several lines, pass a multi-line string:

const result = pyodide.runPython(`
import sys
sys.version
`);
console.log(result);

The result is the Python version string of the bundled interpreter. Each Pyodide release bundles one CPython version, so this is also how you check what you are running.

The Pyodide docs list tested browsers: Firefox 112, Chrome 112 and Safari 16.4. Older browsers may not work properly. If you want a gentler start with the language itself rather than the plumbing, see Hello World in Python.

Install packages in an online interpreter

There is no pip install command in a browser. Pyodide offers two loaders instead, and the docs recommend micropip as the default. It installs pure Python wheels from PyPI, packages built for Pyodide, and wheels from custom URLs, and it resolves dependencies. From Python:

import micropip
await micropip.install("snowballstemmer")

The lower-level pyodide.loadPackage() only loads packages built for Pyodide, such as numpy:

await pyodide.loadPackage("numpy");

The practical rule: a package that is plain Python usually installs from PyPI, and a package with compiled C code only works if someone has built it for WebAssembly and it ships with Pyodide or is supplied as a wasm32 wheel. A custom wheel URL must end in .whl, and the server hosting it must send CORS headers.

What an online Python interpreter can't do

The limits are different for the two kinds. For Pyodide, the documentation lists these constraints:

  • Threads and multiprocessing. threading and multiprocessing can be imported but do not work. Code that depends on them fails or needs a patch.
  • Sockets. The socket module imports but is not functional, so you can't open a raw connection or run a server.
  • HTTP requests. requests and urllib3 work, but every request goes through the browser. You have little control over certificates, timeouts and proxies, and the target server's CORS policy can block the request. A script that scrapes a site often fails for this reason alone.
  • Removed modules. fcntl, grp, pwd and termios are removed, and pty and tty can't be imported because termios is gone.
  • Blocking the page. By default WebAssembly runs on the main thread, so a long computation freezes the page. The docs suggest Web Workers or Service Workers as the workaround.

Server-based interpreters have no single set of limits. Each site decides which packages, run times and network access it allows, so read the site's own notes.

When to use an online interpreter and when to install Python

An online interpreter is the right tool for trying a snippet, checking how a built-in behaves, teaching in a classroom where installs are blocked, or running a short calculation on a phone. It is the wrong tool for anything that reads local files, runs for minutes, talks to databases or serves requests.

For real projects, install Python locally. The steps for Windows are in Install Python on Windows, and there you also get a debugger, virtual environments and full package support.

Privacy: what to paste and what to keep out

With a server-based interpreter, treat everything you type as published to a third party. Don't paste API keys, passwords, tokens, customer data or proprietary code. Even with a browser-based one, the page's own scripts can still send your input elsewhere, because nothing in the sandbox stops a page from sending what you type to its own server. The Network tab check above is the only way to be sure for a given site.

If your code works locally but fails online

Work through these in order:

  1. Print sys.version to confirm the Python version. Newer syntax fails on an older runtime.
  2. Look for import threading, multiprocessing or socket. These are the usual reason for failure in a browser runtime.
  3. Check any third-party import. If it has compiled extensions, it may simply not exist for WebAssembly.
  4. For network calls, open the browser console. A CORS error there means the target server refused the browser's request, and no change to your Python will fix it.
  5. If the page freezes, the code is running on the main thread. Reduce the work or run it where a Web Worker is available.

Frequently asked questions

Is an online Python interpreter safe to use?
It depends on where the code runs. A browser-based interpreter built on WebAssembly runs inside the browser's sandbox, which enforces the same-origin and permissions policies of other web code. A server-based one sends your code to someone else's machine, so do not paste passwords, API keys or private data into it.
Can an online Python interpreter install packages with pip?
Not with pip itself. Pyodide, a common in-browser runtime, provides micropip, which installs pure Python wheels from PyPI and packages built for Pyodide, such as numpy. Packages with C extensions that nobody has built for WebAssembly can't be installed.
Why does my online Python code freeze the page?
By default, Pyodide runs WebAssembly on the browser's main thread, so a long computation blocks the page until it finishes. Running Pyodide inside a Web Worker keeps the page responsive.
Which Python version does an online interpreter run?
Whichever version the site chose. Each Pyodide release bundles one specific CPython version, and server-based sites pick their own. Check the version printed by sys.version before relying on newer syntax.

Sources