Registry / http-networking / sniffio

sniffio

JSON →
library1.3.1pypypi✓ verified 24d ago

A Python library that detects which asynchronous I/O library (e.g., Trio, asyncio) is currently running in your code. Current version: 1.3.1, released on February 25, 2024. Maintained by the Trio project, it has a stable release cadence with updates approximately every 1-2 years.

pip install sniffio
INSTALL
IMPORT
SIG · SNIFFIO
S
sniffio
http-networkingpythonv1.3.1
Install
1.6s avg
Import
15ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.1 · 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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.016s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.014s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

current_async_library
from sniffio import current_async_library
Ensure correct import path to access the function.

A simple script demonstrating how to use sniffio to detect the current async library and run a coroutine accordingly.

import sniffio import trio import asyncio async def print_library(): library = sniffio.current_async_library() print(f'This is {library}') # Prints 'This is trio' trio.run(print_library) # Prints 'This is asyncio' asyncio.run(print_library())
Debug
Known issues
breakingDropped support for Python 3.5 and 3.6 in version 1.3.0
fix
Upgrade to Python 3.7 or later.
affects: 1.3.0
deprecatedExplicit version specifications in USES=python for '3.x+' are deprecated
fix
Remove explicit version specifications in USES=python.
affects: 1.3.0
breakingModuleNotFoundError: No module named 'trio'. The 'trio' package is not installed.
fix
Install the 'trio' package using pip: `pip install trio`.
affects: N/A
breakingModule 'trio' not found. Ensure the 'trio' package is installed in the environment.
fix
Install the 'trio' package using pip (e.g., `pip install trio`) or ensure it's included in your project's dependencies.
affects: all
Errors
Common errors & fixes
RuntimeError: This function is only available from inside a Trio or asyncio task.
You are attempting to call `sniffio.current_async_library()` (or a similar sniffio function) from a synchronous context, outside of an active Trio or asyncio event loop.
fix
Ensure that calls to `sniffio.current_async_library()` are made within an `async` function that is itself executed by an asynchronous runner like `asyncio.run()` or `trio.run()`.

```python
import sniffio
import asyncio

async def my_async_function():
    print(f"Current async library: {sniffio.current_async_library()}")

asyncio.run(my_async_function())
```
ModuleNotFoundError: No module named 'sniffio'
The `sniffio` package has not been installed in your current Python environment.
fix
Install the `sniffio` package using pip.

```bash
pip install sniffio
```
AttributeError: module 'sniffio' has no attribute 'async_library'
You are attempting to access a non-existent attribute or made a typo when trying to call the correct function to detect the current async library.
fix
Use the correct function name `sniffio.current_async_library()` to retrieve information about the running async library.

```python
import sniffio
import asyncio

async def get_library_info():
    lib = sniffio.current_async_library()
    if lib:
        print(f"Detected: {lib.sniffio_name}")
    else:
        print("No async library detected.")

asyncio.run(get_library_info())
```
AttributeError: 'NoneType' object has no attribute 'sniffio_name'
This error occurs when `sniffio.current_async_library()` returns `None` (indicating no async event loop is running), and you then attempt to access an attribute like `sniffio_name` directly on that `None` value without checking if a library was actually detected.
fix
Always check if the return value of `sniffio.current_async_library()` is not `None` before attempting to access its attributes.

```python
import sniffio
import asyncio

async def check_and_use_library():
    detected_lib = sniffio.current_async_library()
    if detected_lib:
        print(f"Running with: {detected_lib.sniffio_name}")
    else:
        print("No async library is currently active.")

asyncio.run(check_and_use_library())
```
Upgrade
Version history
1.3.1latest on PyPI · released Feb 25, 2024
Audit
Dependencies
triorequiredRequired for Trio support
asynciorequiredRequired for asyncio support
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
sniffio — pip install sniffio · libregistry