devkult_
tools30converters34

.py MIME type

Python sourcetext & code

text/x-python

Python source files. Unregistered; text/x-python is what Python's own mimetypes module returns.

MIME typetext/x-python
Extensions.py
Content-Typetext/x-python; charset=utf-8
Compress in transit?Yes — enable gzip or Brotli

Good to know

  • A web server sends .py files as plain downloads unless a CGI or WSGI handler is configured for them — keep source with secrets out of the web root.
  • Compiled bytecode (.pyc) is application/x-python-code.

Serve .py files with the right type

The response header to send:

Content-Type: text/x-python; charset=utf-8

Most servers pick the type from the file extension. If yours sends the wrong one, map it explicitly:

nginx (inside the types block in mime.types)
text/x-python py;
Apache (.htaccess or server config)
AddType text/x-python .py
Express
res.set("Content-Type", "text/x-python; charset=utf-8");
Python
import mimetypes

mimetypes.add_type("text/x-python", ".py")
HTML file input
<input type="file" accept=".py,text/x-python">

Check it

See what a server sends for a file, and what type a local file really is by its contents:

curl -sI https://example.com/file.py | grep -i '^content-type:'
file --mime-type file.py

Frequently asked questions

What is the MIME type for .py files?

text/x-python.

What Content-Type header should a .py file be served with?

Content-Type: text/x-python; charset=utf-8. As a text format, include the charset so browsers don't guess the encoding. It compresses well, so let the server apply gzip or Brotli.

Related types

Headers that go with it: Content-Type · Content-Disposition · X-Content-Type-Options