varname
Dark magics about variable names in python.
What it is and what it does
Varname is a metaprogramming library that uses stack frame inspection to retrieve the names of variables that function or class calls are assigned to, from inside those functions or classes. It exposes this core capability through functions like `varname()` (to get the assignment variable name), `argname()` (to fetch the names or source of arguments passed to a function), and `will()` (to detect the next attribute access). The library depends on executing for bytecode inspection and typing-extensions for type hints.
The package is useful when you need runtime introspection of variable names—for instance, to automatically populate an object's `__varname__` attribute with the name it was assigned to, to create dictionaries without explicitly repeating key names, or to implement debugging utilities that print variable names alongside their values. It handles complex cases like multiple assignment, nested unpacking, and calls wrapped through intermediate functions.
Use it for:
- Automatically populate an object's id or name field with the variable name it was assigned to.
- Create dictionaries from variables without repeating key names (jsobj helper).
- Implement debug utilities that print variable names and values together.
- Fetch the names or source expressions of arguments passed to a function.
- Detect which attribute will be accessed next on an object (will function).
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Retrieves variable names at runtime from inside functions and classes, and provides utilities to fetch argument names and detect attribute access patterns.
Yes. The package is actively maintained, has low install friction, carries a permissive MIT license, and solves a real metaprogramming need. It is well-documented with concrete examples. Install it if you need runtime variable name introspection; skip it if your code does not require that capability.
Install
varname on PyPI
pip
pip install varnameuv
uv add varnamepoetry
poetry add varnameInstalling varname
Before you install
Low install friction with only two runtime dependencies (executing and typing-extensions). Actively maintained with recent commits; last release 120 days ago and repository shows 357 stars.
License in practice
MIT license permits free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.
Quickstart
from varname import varname
def function():
return varname()
func = function() # func == 'func'
Requires Python 3.9 or later; earlier versions need varname < 0.11.
Verify before relying
- Whether frame inspection works reliably across all Python 3.9+ implementations and edge cases.
- Performance characteristics when called repeatedly in hot code paths.
- Compatibility with async/await patterns beyond the asyncio.run example shown.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — executing, typing-extensions |
| Maintenance | actively maintained — 120 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 414,920/month — #6,828 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: varname-1.0.0-py3-none-any.whl
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
nameofRetrieves the name of a variable or attribute…
permissive · top 15,000 on PyPI
executingIdentifies the AST node currently being…
permissive · top 1,000 on PyPI
getnameProvides random name generation for cats, dogs,…
permissive · top 15,000 on PyPI
singleton-decoratorProvides a decorator that enforces singleton…
copyleft · top 15,000 on PyPI
patchyPatchy modifies Python functions at runtime by…
permissive · top 5,000 on PyPI
read-versionExtracts a version string or other variable…
permissive · top 15,000 on PyPI
snoopsnoop adds decorator-based and context-manager…
permissive · top 15,000 on PyPI
argbindArgBind binds function and class arguments to…
permissive · top 15,000 on PyPI
publicProvides a decorator-based alternative to…
permissive · top 15,000 on PyPI
icecreamIceCream provides a drop-in replacement for…
permissive · top 5,000 on PyPI