简体   繁体   中英

Printing Unicode in eclipse Pydev console and in Idle

My configuration: Win7 + Python 2.6 + eclipse + PyDev

How do I enable Unicode print statements in:

  1. PyDev console in eclipse
  2. Idle Python GUI

Example print statement:

print(u"שלום עולם")

This comes out as:

ùìåí òåìí

For eclipse unicode console support:

  1. Add -Dfile.encoding=UTF-8 to eclipse.ini which is in the eclipse install directory.
  2. In eclipse - Run\\Run Configurations\\Python Run\\configuration\\Common\\ make sure UTF-8 is selected
  3. In eclipse - Window\\Preferences\\General\\Workspace\\Text file encoding\\ making sure UTF-8 is selected
  4. In [python install path]\\Lib\\site.py - change from encoding = "ascii" to encoding = "utf-8"
  5. Make sure you're using unicode supporting fonts in eclipse - Window\\Preferences\\Appearance\\Colors and Fonts\\Debug\\Console font\\Edit

In the installation I did all of the above:

print(u"שלום עולם")         # Doesn't work
print("שלום עולם")          # Works

For django models:

print(my_model.my_field)                 # Doesn't work
print(my_model.my_field.encode('utf-8')) # Works

I was having the a same problem in Eclipse Luna 4.0.4 with Python 3.4.1 and PyDev 3.6.0. I tried the steps given above, and a few others, and was getting nowhere.

What worked for me was, in Eclipse, in Preferences —> PyDev —> Interpreters —> Python Interpreter , in the Environment tab, I added the environment variable PYTHONIOENCODING and specified its value as utf-8 .

That did the trick for me…

PYTHONIOENCODING is a pretty good generic way of fixing this problem. However, the Eclipse way of setting the locale of its console is as follows:

Set the Run Configuration encoding:

  1. Edit Run Configuration
  2. Click on "Common" tab
  3. Set Encoding to "UTF-8"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM