Neutron Lib
raw JSON → 3.25.0 verified Mon Apr 27 auth: no python
neutron-lib is a library of shared routines and utilities for OpenStack Neutron. It provides common code (exceptions, DB models, API definitions, extensions) used by neutron and neutron sub-projects. Version 3.25.0 supports Python >=3.10. Release cadence follows OpenStack coordinated releases, often with multiple minor releases per cycle.
pip install neutron-lib Common errors
error ImportError: cannot import name 'NeutronException' from 'neutron_lib' ↓
cause NeutronException is in neutron_lib.exceptions, not directly in neutron_lib.
fix
Use: from neutron_lib import exceptions
try:
raise exceptions.NeutronException(...)
error AttributeError: module 'neutron_lib' has no attribute 'context' ↓
cause Common mistake: trying to use 'neutron_lib.context' but context module is not automatically imported. You need an explicit import.
fix
Add: from neutron_lib import context
error neutron_lib.exceptions.NeutronException: Object not found ↓
cause This error is raised when a resource is not found (like a port, network, etc.) but the caller may not realize it's an exception from neutron-lib.
fix
Catch neutron_lib.exceptions.NeutronException or a more specific subclass like PortNotFound, NetworkNotFound.
Warnings
breaking In neutron-lib >= 2.0.0, the DB model classes were moved into separate database backend modules. Direct imports from neutron_lib.db.model_base may be deprecated or changed. ↓
fix If using neutron_lib.db.model_base, check the OpenStack release notes for the exact new import path, e.g., from neutron_lib.db import model_base as mb.
breaking The api.attributes module was refactored in some versions; constants like ATTR_NOT_SPECIFIED were moved to neutron_lib.api.definitions. ↓
fix Use from neutron_lib.api.definitions import ATTR_NOT_SPECIFIED instead.
gotcha Do not install both neutron and neutron-lib from different versions in the same environment. They must be from the same OpenStack release series to avoid API incompatibility. ↓
fix Ensure that the versions of neutron and neutron-lib are consistent (e.g., both from the same OpenStack release).
Imports
- context wrong
from neutron import contextcorrectfrom neutron_lib import context - exceptions wrong
from neutron.common import exceptionscorrectfrom neutron_lib import exceptions
Quickstart
from neutron_lib import context
from neutron_lib import exceptions
# Create a request context
req_context = context.RequestContext(user_id='admin',
tenant_id='admin',
roles=['admin'])
print(req_context.to_dict())
# Raise a NeutronException
try:
raise exceptions.NeutronException(message='example')
except exceptions.NeutronException as e:
print(e)