3

I have a python code that prints Arabic text on vscode terminal, the output in the terminal appears in "unreadable" format.

code:

print('مرحبا')

output:

مرحبا

How can I solve this issue?

Minions
  • 5,104
  • 5
  • 50
  • 91
  • There is a lengthy discussion/answer on this here: https://stackoverflow.com/questions/39528462/python-3-print-function-with-farsi-arabic-characters – Thaer A Apr 12 '20 at 09:44
  • @ThaerA , I tried it before but it doesn't work. When I use pycharm or other emulators the arabic chars appear in a right way. I only have this issue with vscode. – Minions Apr 12 '20 at 09:52
  • Declaring the source file encoding seems to solve the problem to some extent. Still the characters are aligned LTR. Add this at the top of your py file (in two commented lines).. #!/usr/bin/env python # -*- coding: UTF-8 -*- – Thaer A Apr 12 '20 at 10:04
  • @ThaerA unfortunately neither this way. even the arabic chars don't appear in the right format. – Minions Apr 12 '20 at 10:10
  • Try this: there is a UTF button at the bottom right of VSCode, click that > click save with encoding > pick Arabic (Windows-1256. Now in the terminal type > chcp 1256 ... This should display arabic text but the letters will still be LTR – Thaer A Apr 12 '20 at 10:29
  • @ThaerA I did it: `SyntaxError: Non-UTF-8 code starting with '\xe3' in file c:\Users\ghanem\project1\playground.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details` – Minions Apr 12 '20 at 10:36
  • Did you type: chcp 1256 in the terminal and got "Active code page: 1256" before running the script (that was saved in Arabic Windows 1256) ? – Thaer A Apr 12 '20 at 10:41
  • Sorry, add #!/usr/bin/env python # -*- coding: windows-1256 -*- at the top of the file too. IN TWO LINES. – Thaer A Apr 12 '20 at 10:42
  • The same result @ThaerA .. thanks for your efforts. it looks that it's something more complicated. – Minions Apr 12 '20 at 10:47
  • IT IS. Powershell doesn't support Arabic. That's the problem. – Thaer A Apr 12 '20 at 10:48
  • Is this a problem in the terminal? If so what shell are you using? – Brett Cannon Apr 15 '20 at 22:48
  • @BrettCannon I tried all of the available ones (windows shell, cmd, git bash). – Minions Apr 16 '20 at 09:21
  • What font do you have set for your terminal? Is it one that supports Arabic? – Brett Cannon Apr 21 '20 at 21:24
  • I didn't change the font. Is there specific font type? – Minions Apr 22 '20 at 16:37
  • Does this answer your question? [How to get RTL (right-to-left) text working in VS Code integrated terminal?](https://stackoverflow.com/questions/75985838/how-to-get-rtl-right-to-left-text-working-in-vs-code-integrated-terminal) – starball Aug 31 '23 at 05:23

2 Answers2

0

You need to import arabic-reshaper library: Type in your system command line "pip install --upgrade arabic-reshaper". Then use import arabic_reshaper in your IDE. After that: : "pip install python-bidi". And use from bidi.algorithm import get_display in your IDE.

nc_6d
  • 25
  • 7
  • I did the same steps, also I restarted the IDE but it doesn't work. – Minions Apr 12 '20 at 09:34
  • I've just found the similar problem with C# files, so try to do these actions with Python settings https://stackoverflow.com/questions/6828148/how-to-open-arabic-text-file-with-correct-encoding-in-visual-studio – nc_6d Apr 12 '20 at 09:38
  • This is different. The link you mentioned is regarding a question for visual studio (not vscode). – Minions Apr 12 '20 at 09:42
0

Yum Just need to Change the Encoding. On the the status bar down click on UTF-8 --> save with encoding --> the choose you encodeing (i think windows 1256)