I have recently started to use python 3.4 on Ubuntu and installed python 3.4 IDLE.
I have used the IDLE before on Windows and it works fine and correctly displays Farsi or Arabic which are written right-to-left.
However, on Ubuntu when I try to print a Farsi word on python IDLE it does not show the word correctly.
Here is an example, the correct rendering of word is:
مجلس
but on IDLE I see
س ل ج م
Is this something related to Ubuntu or IDLE and how can I fix it?
# -*- coding: utf8 -*-
print(u"مجلس")
You need to add a header for utf8 on top to add support for those characters.
Related
The Python 3.4 and Python 3.8/3.9 are different when I try execute below statement:
print('\u212B')
Python 3.8/3.9 can print it correctly.
Å
Python 3.4 will report an exception:
Traceback (most recent call last):
File "test.py", line 9, in <module>
print('\u212B')
UnicodeEncodeError: 'gbk' codec can't encode character '\u212b' in position 0: illegal multibyte sequence
And according to this page, I can avoid the exception by overwrite sys.stdout via statement:
sys.stdout = io.TextIOWrapper(buffer=sys.stdout.buffer,encoding='utf-8')
But python 3.4 still print different charactor as below:
鈩?
So my questions are:
Why do different python versions have different behaviors on stand output print?
How can I print correct value Å in python 3.4?
Edit 1:
I guess the difference is caused by PEP 528 -- Change Windows console encoding to UTF-8. But I still don't understand the machanism of console encoding and how I can print correct character in Python 3.4.
Edit 2:
One more difference, sys.getfilesystemencoding() will get utf-8 in Python 3.8/3.9 and get mbcs in Python 3.4.
Why?
Regarding the rationale behind the stdout encoding you can read more in the answers here: Changing default encoding of Python?
In short, Python 3.4 is using your OS's encoding by default as the one for stdout whereas with Python 3.8 it is set to UTF-8.
How to fix this?
You can use a new method - reconfigure introduced with Python 3.7:
sys.stdout.reconfigure(encoding='utf-8')
Typically, you can try setting the environment variable PYTHONIOENCODING to utf-8:
set PYTHONIOENCODING=utf8
in most of the operating systems except Windows where another environment variable must be set for it to work:
set PYTHONLEGACYWINDOWSIOENCODING=1
You can fix it in the version of Python preceding v. 3.7 via installing win-unicode-console package that handles UTF issues transparently on Windows:
pip install win-unicode-console
If you are not running the code directly from a console there is a possibility that your IDE configuration is interfering.
Recently I'm considering using mainly python 3 although I have used python 2.7 so far.
But I encountered variable problem on python 3 as follows.
For example, the below code properly works in python 2.
#print a
a=1
I run the code named test.py many times on ipython console (python 2.7.16) in spyder 3.3.6.
After the first run, I remove # in the first line.
Then, ipython console outputs 1 which is a's value.
However, when I run the above code (the first line is replaced by #print(a)) similar to the above on ipython console (python 3.7.6) in spyder 4.0.1, ipython console outputs an error message,
NameError: name 'a' is not defined.
When I input a in the ipython console, the console outputs 1.
Can I do the same thing in my python 3 environment as I do in python 2?
Thank you in advance.
There is a setting when running the script called "Run in console's namespace instead of an empty one".
If you tick that box it keeps the variables in the namespace.
I have to do a project with turkish content. In my ubuntu machine with python 3.6.5, I haven't any problem with this. But in production server, that is a debian machine with the same python 3.6.2, I have:
SyntaxError: Non-UTF-8 code starting with ....
When I use # -*- coding: utf-8 -*- as docs, I have:
utf-8 can't decode byte
I have searched google and stackoverflow all day. And try all suggestions.
Any advice about this issue would be appreciated.
I'm still learning python. I have windows 8 and had downloaded 2.7 as well as 3.5 because two modules I used in the past respectively used different python versions. Now though, I'm trying to run a script where the first line is import http.client or http. Neither of these work though. In cmd, python returns 3.5.1 currently and import http.client and import http return with no errors but in my IDE these don't work. Why not?
You can follow the Pycharm documentation here to change the Python version for your project from Python 2 to Python 3.
(In particular, Selecting Python interpreter for a project section)
I have both 2.7 and 3.0 versions of the Python interpreter installed (on my Ubuntu 32 system), but one particular script uses 3.0.
Using
#!/usr/bin/python3 -B
will not work when the program is run with python myprogram.py.
And I also need a solution that works also in Windows where I also have both python versions installed.
How can I make the script to run only with the right python version?
Please use virtualenv, which makes isolated Python environments easy.
python = Python to use. # This has to be the absolute path to Python executable
os.execl(python, python, * sys.argv)
This way you can restart the script with the python you want to use. Not really stylish.
I don't know why you can't just launch the program with python3 foo.py, but it's possible to have a python2 program relaunch itself as python3 with something like this.
import sys
if sys.version_info.major != 3:
import os
# replace this process with a python3 process
os.execlp("python3", "python3", *sys.argv)
It's a bad solution though, because now your python3 program can't use anything that's not valid python2 syntax
Please take a look at The wrong python interpreter is called
You have to choose a correct interpreter based on where you installed the desired version of Python and your system variables.