# .py MIME Type: text/x-python

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

- MIME type: `text/x-python`
- Extensions: .py
- Content-Type header: `text/x-python; charset=utf-8`
- Compress in transit: yes

## 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`.

## Server configuration

### 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">
```

## FAQ

### 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

- [.sh — application/x-sh](https://www.devkult.com/mime/sh.md)
- [.txt — text/plain](https://www.devkult.com/mime/txt.md)
- [.json — application/json](https://www.devkult.com/mime/json.md)

Open the interactive page: https://www.devkult.com/mime/py