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 - plus keyed hiding of string literals (URLs, tokens, messages) and import names.

PyPI Python versions License

Install  |  Quick start  |  CLI  |  API  |  Configuration  |  Examples


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. The result is a source-to-source transformation that keeps behaviour intact while changing every name it can safely change.

from pynamer import rename

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

How values are hidden

Renaming changes names; it leaves literal values readable. PyNamer closes that gap:

  • String literals ("https://...", API tokens, log messages, dict keys, f-string text, docstrings and string annotations) are replaced with a keyed, hex-encoded token and a single module-level decoder is injected at the top of the file. The key is derived from the seed, so the same input always produces the same output. Behaviour is untouched - every token decodes back to the exact original string at runtime.
  • Import statements are rewritten to __import__ indirection, so the module names themselves disappear from the source: import requests as r becomes r = __import__(<decoded "requests">).

Both passes are on by default and can be switched off with hide_strings=False / hide_imports=False, or from the CLI with --no-hide-strings / --no-hide-imports.

source = 'API = "https://api.example.com/v1"\nprint(API)'
print(rename(source))
_0x2 = b'H\xd0d\xd7\xf6\x9e\xb0q\xd0\xcb\xca\xe8\xaar\x1c\xb0'
def _0x0(t):
 return bytes((a ^ _0x2[i % len(_0x2)] for i, a in enumerate(bytes.fromhex(t)))).decode()
_0x1 = _0x0('20a410a785a49f5eb1bba3c6cf0a7ddd38bc01f995f1dd5ea6fa')
print(_0x1)

Install

pip install pynamerx

Requires Python 3.10 or newer.

Quick start

The simplest entry point takes source in and returns source out:

from pynamer import rename

source = '''
def greet(name):
    message = "hello, " + name
    return message

print(greet("world"))
'''

print(rename(source))

Need more than a string? Use process to get the mapping, stats and the resulting tree as well:

from pynamer import process

result = process(source, strategy="hash", seed=1, builtins=True)

print(result.stats.as_dict())   # symbol / attribute / size statistics
print(result.mapping.symbols)   # {"greet": "_0x3", ...}
result.mapping.save("map.json")

Command line

pynamer module.py -o module.obf.py --prefix _0x --strategy base62
pynamer module.py --builtins --hide-init --strip-docstrings --stats
pynamer module.py --no-hide-strings --no-hide-imports   # only rename names
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
hide_strings True encode string literals behind an injected runtime decoder
hide_imports True rewrite import statements to __import__ indirection
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

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
String literals left readable keyed encode + injected runtime decoder (URLs / tokens / messages, docstrings and annotations hidden)
Imports module names visible in source rewritten to __import__ indirection
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

What is deliberately not renamed

Expand the list of protected names
  • __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.
  • String literals that must stay literal for the program to compile: match patterns (case "x":, mapping keys) and PEP 695 type alias values. Everything else - docstrings, annotations / forward references, and f-string text while leaving format specs intact - is encoded.
  • 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

Expand the list of caveats

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

  • A renamed class / function reports its new __name__ / __qualname__, and an Enum member's .name reflects the new spelling. Because docstrings are encoded by default, __doc__ becomes None (and is dropped entirely with strip_docstrings=True); annotations are stored as decoder calls, so the raw __annotations__ text differs, though typing.get_type_hints still resolves the same values. 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).
  • hide_imports leaves relative imports (from . import x) and from ... import * untouched, since their module name is resolved by the package at runtime.
  • hide_strings trades size for secrecy: encoded literals are larger than the originals they replace.

Examples

Runnable examples live in examples/:

python examples/demo.py                                   # end-to-end demonstration
python examples/obfuscate.py examples/demo.py             # obfuscate & verify a real file
python examples/obfuscate.py examples/demo.py -o out.py --builtins
  • examples/demo.py - a self-contained demonstration that renames an embedded module with several configurations and asserts the behaviour never changes.
  • examples/obfuscate.py - obfuscates a given file through the library API and verifies the obfuscated module still produces identical output.

Metadata

Release files for pynamerx 0.2.1

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.2.1
File Size Uploaded Provenance Actions
pynamerx-0.2.1.tar.gz 28.3 kB With provenance Details for pynamerx-0.2.1.tar.gz

Built distribution (wheel)

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

Total release size: 55.9 kB

Release files / pynamerx-0.2.1.tar.gz

Download URL pynamerx-0.2.1.tar.gz
Size 28.3 kB
Tags Source
SHA-256 checksum
How to use checksums
051453fe40e63573e27847bc9cb85badb7488f4271d5195796c2b52c0ad730e4
BLAKE2b-256 checksum
How to use checksums
1ed1ea0e514b4720e6719d97088d5def90f1ab38a4ffb7dd6abef9030d1c6de1
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.2.1-py3-none-any.whl

Download URL pynamerx-0.2.1-py3-none-any.whl
Size 27.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf66709e356dffa8d491d7f7c03dcc006a9f6fd787bd30d2b9dfe7a5e37bd818
BLAKE2b-256 checksum
How to use checksums
11cec80441cc22372326044d64baa7e8fd381fedeb59760e21c0787498619478
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.2.1

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.2.1.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