Install & Compatibility
Where this runs
tested against v6.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.635s · 24.3MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.8s · import 0.550s · 25MB
26MB installed
● package 26MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Service
✓ from cornice import Service
✗ from cornice.service import Service
Service is directly exposed at the top level of the cornice package.
resource
✓ from cornice.resource import resource
Used to define resources with multiple services and CRUD operations.
validators
✓ from cornice import validators
Access to built-in validators, often used with Colander schemas.
This quickstart demonstrates how to define a simple 'Hello World' service using Cornice within a standalone Pyramid application. Run this script, then access `http://0.0.0.0:6543/hello` in your browser or with `curl`.
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from cornice import Service
# 1. Define a Cornice service
hello_service = Service(name='hello', path='/hello', description="Hello World Service")
@hello_service.get()
def get_hello(request):
"""Returns a 'Hello World!' message."""
# Access request data via request.GET, request.json, request.validated, etc.
return {'message': 'Hello World! from Cornice!'}
# 2. Integrate into a Pyramid application
if __name__ == '__main__':
with Configurator() as config:
# Include the cornice package to register its components
config.include('cornice')
# Add the defined Cornice service to the Pyramid configuration
config.add_cornice_service(hello_service)
# Create a WSGI application
app = config.make_wsgi_app()
# 3. Run the Pyramid application
server = make_server('0.0.0.0', 6543, app)
print('Serving on http://0.0.0.0:6543/hello')
print('Press Ctrl+C to quit')
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.server_close()
Errors
Common errors & fixes
ImportError: cannot import name 'Service' from 'cornice'
Incorrect import path; 'Service' is directly available in the top-level 'cornice' package, not a submodule.
fixChange the import to `from cornice import Service`.
AttributeError: 'Service' object has no attribute 'add_get'
Attempting to use older, deprecated `add_` methods (e.g., `add_get`, `add_post`) instead of the decorator syntax for defining service methods.
fixUse the decorator syntax for defining service methods, e.g., `@service.get()` or `@service.post()`.
TypeError: 'Request' object is not subscriptable
Attempting to access request attributes like `request['json']` or `request['validated']` using dictionary-style bracket notation.
fixAccess request data using dot notation: `request.json` or `request.validated`.
colander.Invalid: ... missing or invalid fields ...
Validation failed because the request body or parameters do not conform to the defined Colander schema. This often indicates missing required fields or incorrect data types.
fixReview your Colander schema definition and ensure the incoming request data (e.g., JSON body, query parameters) matches the expected structure and types. Check logs for specific field errors.
Upgrade
Version history
6.1.0latest on PyPI · released Feb 8, 2024
Audit
Dependencies
pyramidrequiredCore web framework integration.
colanderoptionalOptional, for declarative schema validation of request data.
marshmallowoptionalOptional, for declarative schema validation of request data. Requires Marshmallow >= 3.0 since Cornice 6.0.0.