Skip to main content

py2hy is a library and command-line interface to translate Python code to Hy code. As with Hy’s built-in hy2py, all style information is discarded, including most comments. The result can be messy, but it works, and it makes a good starting point for a hand translation. You can also use py2hy when still learning Hy, to help figure out how to do something in Hy given an example in Python.

Usage

To use the command-line interface, see python3 -m py2hy --help. The programmatic interface comprises two functions, of which see the docstrings:

  • py2hy.ast_to_models

  • py2hy.ast_to_text

The test suite uses pytest.

To autoformat the output, try the third-party library beautifhy:

$ python3 -m py2hy mycode.py | beautifhy -

Unimplemented nodes

The following features of Python’s ast are not yet implemented, and are unlikely to get implemented unless I find myself wanting them for some reason. I’ll accept patches for them, though.

  • type_comment for these node types: For, AsyncFor, With, AsyncWith

  • Type aliases: TypeAlias, TypeIgnore, TypeVar, ParamSpec, TypeVarTuple

  • TryStar

  • Pattern-matching: Match, MatchValue, MatchSingleton, MatchSequence, MatchMapping, MatchClass, MatchStar, MatchAs, MatchOr

Version history

Here are the most important user-visible changes in each release.

  • 0.3.0 (2026-05-24): Added support for t-strings, lazy imports, and unpacking in comprehensions.

  • 0.2.1 (2026-01-14): Fixed some bugs with default arguments and class decorators.

  • 0.2.0 (2025-06-05): ast_to_models and ast_to_text got a new parameter allow_unimplemented. An anthill’s worth of bugs have been exterminated.

  • 0.1.0 (2025-05-08): First release.

License

This program is copyright 2026 Kodi B. Arfer.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Metadata

Release files for py2hy 0.3.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 py2hy 0.3.0
File Size Uploaded Provenance
py2hy-0.3.0.tar.gz 7.3 kB Without provenance Details

Release files / py2hy-0.3.0.tar.gz

Download URL py2hy-0.3.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
559d38a0368aba6a947da37c8b422ab1f9f80210b0d8f79c50977025970d6631
BLAKE2b-256 checksum
How to use checksums
b67ef7a6d37376983787b6b9e55aa20be7737036d8f1c83f39398f90e54a6aa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for py2hy 0.3.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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