{"id":9424,"library":"zope-contenttype","title":"Zope ContentType","description":"Zope ContentType is a utility module within the Zope ecosystem designed for handling MIME types, particularly the 'Content-Type' HTTP header. It provides functionalities for guessing content types from filenames and (optional) data, determining content types from text, and parsing MIME type strings. Currently at version 6.0, the library follows semantic versioning with new minor releases approximately every 2-6 months and major releases every 2-3 years, in line with the broader Zope project.","status":"active","version":"6.0","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/zope.contenttype","tags":["zope","content-type","mime-type","http","utility","web"],"install":[{"cmd":"pip install zope.contenttype","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"guess_content_type","correct":"from zope.contenttype import guess_content_type"},{"symbol":"parse_content_type","correct":"from zope.contenttype import parse_content_type"},{"note":"The ContentType class is directly importable from the top-level package in recent versions.","wrong":"from zope.contenttype.contenttype import ContentType","symbol":"ContentType","correct":"from zope.contenttype import ContentType"}],"quickstart":{"code":"from zope.contenttype import guess_content_type, parse_content_type\n\n# Guess content type from a filename and optional data\nfilename = 'report.pdf'\ncontent_data = b'%PDF-1.4...'\ncontent_type, encoding = guess_content_type(filename, content_data)\nprint(f\"Guessed content type for '{filename}': {content_type}, encoding: {encoding}\")\n\nfilename_text = 'document.txt'\ntext_data = b'This is a plain text document.'\ntext_type, text_encoding = guess_content_type(filename_text, text_data)\nprint(f\"Guessed content type for '{filename_text}': {text_type}, encoding: {text_encoding}\")\n\n# Parse a content type string\ncontent_type_str = 'text/html; charset=utf-8'\nparsed_type = parse_content_type(content_type_str)\nprint(f\"Parsed content type '{content_type_str}':\")\nprint(f\"  Primary type: {parsed_type.media_type.primary}\")\nprint(f\"  Sub type: {parsed_type.media_type.sub}\")\nprint(f\"  Charset: {parsed_type.charset}\")\n\n# Example of guessing from text (no filename hint)\nhtml_snippet = b'<html><body>Hello</body></html>'\nhtml_type, _ = guess_content_type(None, html_snippet)\nprint(f\"Guessed content type for HTML snippet: {html_type}\")","lang":"python","description":"Demonstrates how to guess content types using filenames and binary data, and how to parse a full content type string into its components. This covers the most common use cases for `zope.contenttype`."},"warnings":[{"fix":"Ensure your build tools and environment support PEP 420 native namespace packages. For `zc.buildout`, upgrade to version 5 or newer. Review any explicit `namespace_packages` configurations in `setup.py` files if upgrading from very old versions.","message":"Version 6.0 replaced `pkg_resources` namespace with PEP 420 native namespaces. This change might impact older `zc.buildout` configurations (prior to version 5) that rely on `pkg_resources` for namespace package discovery.","severity":"breaking","affected_versions":">=6.0"},{"fix":"Upgrade your Python environment to version 3.9 or higher to use `zope.contenttype` version 6.0. Python 3.9 is the minimum required version for `zope.contenttype` 6.0.","message":"Version 5.0 dropped support for Python 2.7, 3.5, and 3.6. Version 5.2 further dropped support for Python 3.7 and 3.8.","severity":"breaking","affected_versions":">=5.0"},{"fix":"Ensure that the `file_contents` argument passed to `guess_content_type` is always a `bytes` object (e.g., `b'your data'`) when running on Python 3.","message":"In version 4.0.1, the `file_contents` argument of `guess_content_type` was changed from expecting a string to expecting bytes in Python 3. Passing a string will result in a TypeError.","severity":"breaking","affected_versions":">=4.0.1"},{"fix":"If your application relies on a stricter content type guessing, you may need to implement custom logic or override `guess_content_type` to call `mimetypes.guess_type(strict=True)` directly or filter its results.","message":"Starting with version 5.2, `zope.contenttype` calls the standard library's `mimetypes.guess_type` with the parameter `strict=False`. This change allows the recognition of more content types (e.g., midi, pict, xul, rtf) that might have been ignored in previous versions. This could subtly change content type guessing behavior.","severity":"gotcha","affected_versions":">=5.2"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Convert the string to bytes before passing it, e.g., `guess_content_type(filename, my_string.encode('utf-8'))`.","cause":"Attempting to pass a Python 3 string object as the `file_contents` argument to `guess_content_type` instead of a bytes object.","error":"TypeError: a bytes-like object is required, not 'str'"},{"fix":"Install the package using pip: `pip install zope.contenttype`. If using a virtual environment, ensure it is activated.","cause":"The `zope.contenttype` package is not installed in the current Python environment or the environment is not correctly activated.","error":"ModuleNotFoundError: No module named 'zope.contenttype'"},{"fix":"Update your `zc.buildout` to version 5 or newer, or ensure your project setup correctly handles PEP 420 namespace packages. This generally means removing `namespace_packages` from `setup.py` for modern Python versions.","cause":"Running `zope.contenttype` 6.0 with an older buildout version (e.g., `<5`) or an environment not compatible with PEP 420 native namespace packages.","error":"SyntaxError: invalid syntax (related to namespace packages or pkg_resources)"}]}