Skip to main content

PyNamer

A scope-aware Python source renamer / obfuscator that survives the hard cases: closures, global/nonlocal, keyword arguments, inheritance, comprehensions, walrus, match, __slots__ and attributes.

PyNamer builds an explicit scope tree (much like CPython's own symtable), performs a dedicated attribute data-flow analysis, and rewrites the AST in a single, re-entrant pass.

from pynamer import rename

obfuscated = rename(open("secret.py").read())

Why it is more powerful

Concern Typical renamers PyNamer
Re-entrancy module-level globals full scope tree, no shared state
Attributes rename everything / nothing data-flow: only locally defined attributes, never externally observed ones
Keyword arguments untouched / naive remapped against the resolved callee, MRO-aware, incl. inherited __init__
Inheritance blind MRO walking for constructors & method kwargs
Comprehensions leaky correct Python 3 scoping (first iterable in the enclosing scope)
getattr(obj, "x") broken constant attribute strings remapped too
Names readable _0x1 deterministic or hashed/compacted, collision-free
Minifier monkey-patches the unparser auditable token-aware whitespace pass
Cross-module none RenameMap gives package-wide, stable names
Safety none optional compile() validation, preservation lists

Install

pip install -e .          # from a checkout
# or
pip install -e ".[test]"

Requires Python 3.10+.

CLI

pynamer module.py -o module.obf.py --prefix _0x --strategy base62
pynamer module.py --builtins --hide-init --strip-docstrings --stats
cat module.py | pynamer - --strategy hash --seed 7
python -m pynamer module.py --map-out map.json   # reuse later with --map-in

Library API

from pynamer import process, rename, rename_ast, RenameConfig, NameStrategy, RenameMap

# 1. simplest form -> string in, string out
out = rename(source)

# 2. full result object
result = process(source, strategy="hash", seed=1, builtins=True)
print(result.stats.as_dict())      # {"symbols": ..., "attributes": ...}
print(result.mapping.symbols)      # {"my_func": "_0x3", ...}
result.mapping.save("map.json")

# 3. AST in, AST out
new_tree = rename_ast(result.tree)

# 4. explicit config object
cfg = RenameConfig(strategy=NameStrategy.BASE62, prefix="_", minify=True)
out = rename(source, config=cfg)

# 5. package-wide stable renaming
shared = RenameMap.load("map.json")
for path in ("a.py", "b.py"):
    process(open(path).read(), map=shared)
shared.save("map.json")

Configuration

Every option is a field on RenameConfig and can be passed as a keyword argument to process/rename:

Option Default Meaning
strategy "sequential" sequential / hash / random / base62 / words
seed 0 seed for deterministic strategies
prefix "_0x" prefix for generated identifiers
minify True single-space indentation, no blank lines
strip_docstrings False drop module/class/function docstrings
builtins False alias builtins (print, len, …) at the top of the module
hide_init False obscure explicit constructors behind a forwarding method
track_instances False also rename local Foo()-instance variables
rename_attributes True rename locally defined attributes
rename_kwargs True remap keyword arguments of resolvable calls
preserve () names to never rename
preserve_attributes () attributes to never rename
preserve_public False keep public (non-underscore) module names
validate True re-compile() the result, raising on breakage
map None a RenameMap for cross-module stability

What is deliberately not renamed

  • __dunder__ names and the super builtin (zero-arg super() relies on the literal name for the __class__ cell).
  • visit_* methods by default (they form the ast.NodeVisitor protocol).
  • from __future__ import ... feature names (they are compiler flags).
  • Attributes that also appear on objects PyNamer cannot reason about, and any attribute reached through super() unless it is defined on a local base.
  • Names listed in preserve / preserve_attributes.
  • Parameter names that are passed as a keyword at a call site whose callee cannot be resolved statically (for example receiver.method(x=1) on an unknown object, or any **mapping call) - renaming them would silently break the call, so they keep their spelling.

Known limitations

Renaming is a semantics-preserving source transformation up to reflection:

  • A renamed class/function reports its new __name__ / __qualname__, __doc__ disappears when strip_docstrings=True, and an Enum member's .name reflects the new spelling. Avoid depending on those strings, or list the symbol in preserve / preserve_attributes.
  • Dynamic name lookups via globals(), eval, locals() or non-constant getattr(obj, some_variable) cannot be tracked. Constant getattr/setattr/hasattr/delattr are handled.
  • Attributes of classes whose bases live outside the processed source are left untouched (safe by default).

Examples

Runnable examples live in examples/:

python examples/demo.py                                  # end-to-end demonstration
python examples/obfuscate.py examples/advanced_source.py # obfuscate & verify a file
python examples/obfuscate.py examples/advanced_source.py -o out.py --builtins
  • examples/sample.py - a small Money program used by the demo.
  • examples/advanced_source.py - a deliberately gnarly module (closures, global/nonlocal, kwargs across inheritance, super(), __slots__, match, walrus comprehensions, constant getattr).
  • examples/obfuscate.py - obfuscates a file with the library API and asserts the obfuscated module still produces identical output.

Testing

pip install -e ".[test]"
pytest                      # behaviour-preservation & unit tests
python examples/demo.py     # end-to-end demonstration

License

MIT - see LICENSE.

Metadata

Release files for pynamerx 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pynamerx 0.1.0
File Size Uploaded Provenance Actions
pynamerx-0.1.0.tar.gz 25.4 kB With provenance Details for pynamerx-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pynamerx 0.1.0
File Interpreter ABI Platform Actions
pynamerx-0.1.0-py3-none-any.whl Python 3 none any Details for pynamerx-0.1.0-py3-none-any.whl

Total release size: 49.5 kB

Release files / pynamerx-0.1.0.tar.gz

Download URL pynamerx-0.1.0.tar.gz
Size 25.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f267f31a48c1d870809eeb7b5bce5a847a2d574ce8723aa3aded13c83bfa6aa7
BLAKE2b-256 checksum
How to use checksums
6049fb1848d23b32963d482da6686863b12161c058b4c821d09e1fef404f39e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / pynamerx-0.1.0-py3-none-any.whl

Download URL pynamerx-0.1.0-py3-none-any.whl
Size 24.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e68e5b08fd5c42778947123bbc13d8e4734e7208ae52ad8e1c9d111e06bce91
BLAKE2b-256 checksum
How to use checksums
578059be79fcde7ace776cd7038f47563fd749653557e17c3b5889d99cfc91d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for pynamerx 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pynamerx-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page