Fast Nano ID
Fastnanoid is a tiny, secure, URL-friendly, and highly performant unique string ID generator for Python, leveraging a Rust implementation via PyO3. It provides a drop-in replacement for other Nano ID implementations with a focus on speed. The current version is 0.4.3, with a release cadence that has seen several minor updates and performance improvements over the past year.
Common errors
-
ModuleNotFoundError: No module named 'fastnanoid'
cause The `fastnanoid` package is not installed in your current Python environment.fixRun `pip install fastnanoid` to install the package. -
TypeError: nanoid() takes 0 positional arguments but 1 was given
cause You are attempting to pass the size or alphabet as a positional argument to the `nanoid` function.fixUse keyword arguments: `nanoid(size=10)` instead of `nanoid(10)`, and `nanoid(alphabet="abc")` instead of `nanoid("abc")`. -
error: can't find crate `fastnanoid_py`
cause This error, or similar 'failed to run custom build command' messages, indicates that the Rust compilation process failed during installation, likely due to missing Rust toolchain components or incompatible environment settings.fixInstall the Rust toolchain using `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh` and ensure it's available in your PATH. If issues persist, consult the `maturin` (Rust-Python binding tool) documentation.
Warnings
- gotcha When installing on certain platforms (e.g., specific Linux distributions, ARM architectures, or very new Python versions) for which pre-compiled wheels are not available, pip may attempt to compile `fastnanoid` from source. This requires the Rust toolchain (rustup, cargo) to be installed on the system.
- gotcha The `nanoid` function expects `size` and `alphabet` as keyword arguments. Passing them positionally or using incorrect keyword names will result in a `TypeError`.
Install
-
pip install fastnanoid
Imports
- nanoid
import nanoid
from fastnanoid import nanoid
Quickstart
from fastnanoid import nanoid
# Generate a default Nano ID
id_default = nanoid()
print(f"Default ID: {id_default}")
# Generate a Nano ID with a specific length
id_short = nanoid(size=10)
print(f"Short ID: {id_short}")
# Generate a Nano ID with a custom alphabet and length
id_numeric = nanoid(alphabet="0123456789", size=8)
print(f"Numeric ID: {id_numeric}")