Skip to main content

fa-console

One import. Correct Persian (Farsi) in the Windows console.

English | فارسی

PyPI version Python License: MIT OS

fa_console makes Python's built-in print() and input() work correctly with Persian text on Windows — automatically, with zero required dependencies and no changes to your code beyond a single import.

It fixes both independent layers of the problem:

Layer Problem without fa_console With fa_console
Encoding Legacy console code page (720 / 1252 / …) mangles UTF-8 on redirect or piping; crashes with UnicodeEncodeError Console code page switched to UTF-8 (65001); standard streams reconfigured to UTF-8; child processes pinned via PYTHONUTF8
Rendering Classic console host (conhost) has no Arabic shaping and no bidirectional reordering — correct text is drawn disconnected and backwards Every write is converted to visual order (letter shaping + bidi) so text appears joined and right-to-left. While typing, fa_input echoes correctly in real time

Before

>>> print("سلام دنیا")
ﺎﯿﻧﺩ ﻡﻼﺳ          # disconnected, reversed, unreadable

After

>>> import fa_console
>>> print("سلام دنیا")
سلام دنیا          # joined, right-to-left, readable

Features

  • One-line setupimport fa_console configures everything at import time
  • Encoding layer — UTF-8 code page, UTF-8 sys.stdin/sys.stdout/sys.stderr (neutralises even a broken PYTHONIOENCODING), and pinned environment variables for spawned subprocesses
  • Rendering layer — logical→visual transformation (letter shaping into Unicode Arabic Presentation Forms + bidirectional reordering) applied to all output, including plain print() and f-strings
  • Two transformation backends — uses arabic-reshaper + python-bidi when installed (full UAX #9, lam-alef ligatures, Persian rules); otherwise a built-in, dependency-free engine
  • Live-typing echofa_input() renders letters joined and right-to-left while you type, with built-in-input()-compatible semantics (KeyboardInterrupt / EOFError)
  • Smart detection — Windows Terminal and redirected files/pipes are left untouched; the VS Code integrated terminal gets the same visual transform as the classic console (its xterm.js engine lacks reliable bidi reordering)
  • Fail-open design — the module never crashes the host application; every internal failure is logged through the fa_console logger and degrades gracefully to standard Python behaviour
  • Zero required dependencies — pure standard library, Python 3.8+

Installation

pip install fa-console

Optional, higher-fidelity rendering backend:

pip install "fa-console[high-fidelity]"   # arabic-reshaper + python-bidi

Or simply copy fa_console.py into your project — it is intentionally a single, self-contained module.

Quick start

import fa_console                      # everything is configured here

print("سلام دنیا!")                    # renders correctly everywhere
name = fa_console.fa_input("نام شما: ")  # live, correct echo while typing
print(f"سلام، {name} جان!")            # logical data, correct display

The value returned by input() / fa_input() is always logical-order Unicode: comparisons, len(), slicing, regexes and file writes all see plain, correct Persian text. Visual transformation is a display-time-only concern.

API overview

Member Purpose
setup_console(force=False) Apply the full configuration; idempotent. Returns a ConsoleSetupReport
fa_print(*args, **kwargs) Persian-safe drop-in for print()
fa_input(prompt="") Persian-safe input() with live correct echo
is_visual_mode() True when the display transform is active
get_console_info() Dict snapshot of the detected environment (great for bug reports)
VisualStream The transparent stream wrapper (advanced use)
FaConsoleError Deliberate low-level failures (consumed internally — fail-open)

Self-diagnostics

python fa_console.py

Prints a full environment report: encodings, console code page, detected terminal type, active bidi backend and a rendered sample.

How it works

  1. Encoding layerSetConsoleCP/SetConsoleOutputCP (Win32) switch the console to UTF-8; sys.stdin/sys.stdout/sys.stderr are reconfigured to UTF-8; PYTHONUTF8=1 / PYTHONIOENCODING=utf-8 are pinned for child processes.
  2. Rendering layer — on classic conhost only, a VisualStream wrapper is installed on stdout/stderr. Each write is converted: Arabic/Persian letters are replaced by their contextual presentation forms (isolated / initial / medial / final, including پ چ ژ ک گ ی), then the line is reordered for an LTR-drawing terminal while Latin words and numbers stay upright and brackets are mirrored.
  3. Detection — the transform engages only when stdout is an interactive classic console. Windows Terminal (WT_SESSION), VS Code terminals (TERM_PROGRAM) and redirected output are bypassed, because they render Arabic correctly or must receive standard logical text.

Configuration

Environment variable Effect
FA_CONSOLE_FORCE_VISUAL=1 Force the visual transform on (debugging)
FA_CONSOLE_NO_VISUAL=1 Unconditionally disable the visual transform

Compatibility

Environment Behaviour
cmd.exe / standalone PowerShell (conhost) Encoding fixed + visual transform + live typing echo
Windows Terminal Encoding fixed; native rendering (module steps aside)
VS Code integrated terminal Encoding fixed + visual transform (xterm.js has no reliable bidi)
JetBrains / IDLE consoles Encoding fixed; module steps aside (non-tty)
Output redirected to file / pipe Standard logical UTF-8 — safe for other tools
Linux / macOS Safe no-op (standard behaviour)

Limitations & FAQ

  • Letters look disconnected while I type in cmd. fa_input() redraws the line correctly as you type; the console's own echo of system prompts is outside any program's control.
  • Perfect native rendering? Use Windows Terminal — the module detects it and lets it do the shaping.
  • Does the transform corrupt my data? No — it is display-only. Values, comparisons and files always hold logical text.
  • Persian looks wrong in the VS Code terminal. That is an xterm.js limitation — this package compensates automatically by applying the same visual transform used for the classic console (see FA_CONSOLE_NO_VISUAL=1 if you ever need to switch it off).
  • Other RTL languages? The shaping tables cover Arabic and Persian letters; the engine is language-agnostic for the letters it knows.

Development

git clone https://github.com/Padandish/fa-console.git
cd fa-console
python -m pip install build
python -m build          # creates dist/

Run the interactive example in any console:

python example.py

Run the full environment diagnostics:

python fa_console.py

Author

Alireza Hosseinialireza.hosseini@hotmail.com

Tech channels

Follow TechInsightsHub for updates, tutorials and related projects:

Platform Link
Bale ble.ir/TechInsightsHub
Eitaa eitaa.com/TechInsightsHub
Aparat aparat.com/TechInsightsHub

License

MIT © Alireza Hosseini

Release files for fa-console 2.0.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 fa-console 2.0.1
File Size Uploaded
fa_console-2.0.1.tar.gz 18.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fa-console 2.0.1
File Interpreter ABI Platform
fa_console-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 37.8 kB

Release files / fa_console-2.0.1.tar.gz

Download URL fa_console-2.0.1.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0b6ec318a8688af74f235834031990e2d2426c131152a5a0dc64c24f8112a4bf
BLAKE2b-256 checksum
How to use checksums
48b6a78171b73133a5f76c3261d003498f067eb540a22feeaecaa5ba3a556a64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 8, 2026.

Transparency log

Release files / fa_console-2.0.1-py3-none-any.whl

Download URL fa_console-2.0.1-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8a7709f38c0c476fa2f136eae8c09e8628a347393777798488630b09a573150e
BLAKE2b-256 checksum
How to use checksums
041f8e163399a866086a3bc45231a09513bbdad83fb2ca0b6ac900363a7370a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 8, 2026.

Transparency log

Release history Release notifications | RSS feed

2.2.0

2 release files

2.1.0

2 release files

2.0.2

2 release files

This release

2.0.1 This release

2 release files

2.0.0

2 release files

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