Artem Mezhenin
Artem Mezhenin

Reputation: 5757

unicode string tolerance in Eclipse+PyDev

I'm using Eclipse+PyDev to write code and often face unicode issues when moving this code to production. The reason is shown in this little example

a = u'фыва '\
'фыва'

If Eclipse see this it creates unicode string like nothing happened, but if type same command directly to Python shell(Python 2.7.3) you'll get this:

SyntaxError: (unicode error) 'ascii' codec can't decode byte 0xd1 in position 0: ordinal not in range(128)

because correct code is:

a = u'фыва '\
u'фыва'

But because of Eclipse+PyDev's "tolerance" I always get in trouble :( How can I force PyDev to "follow the rules"?

Upvotes: 2

Views: 1415

Answers (5)

Vit Bernatik
Vit Bernatik

Reputation: 3802

It may not be what you are asking. But for my case I got these UTF-8 characters by accident copying my code from various sources. To find what character is making troubles I did in my Eclipse Mars:

Edit->set encoding
other->US ASCII

then I tried to save my file. And I got modal window telling me "Save problems". There was button "Select First Character" It showed me troubling character and I just deleted that character and typed ASCII one.

Upvotes: 0

Michael Galaxy
Michael Galaxy

Reputation: 1283

This solved the problem for me in my source code without having to modify the pydev sitecustomize.py file:

import sys
reload(sys).setdefaultencoding("utf-8")

You could use "ascii" or whatever other encoding you wanted to use.

In my case, the when I ran the program on the command-line, PyDev was using "utf-8", whereas the console was incorrectly setting "ascii".

Upvotes: 0

Ronan Jouchet
Ronan Jouchet

Reputation: 1333

This issue should be fixed in PyDev 3.4.0 (not released yet). Fabio (PyDev maintainer) says: "from now on PyDev will only set the PYTHONIOENCODING and will no longer change the default encoding". And PYTHONIOENCODING is supported since Python 2.6.

Here is the commit on GitHub.

Upvotes: 2

Artem Mezhenin
Artem Mezhenin

Reputation: 5757

This happens because the encoding for the console is utf-8.

There's currently no way to set that globally in the UI, although you can change it by editing: \plugins\org.python.pydev_2.7.6\pysrc\pydev_sitecustomize\sitecustomize.py

And just remove the call to: (line 108) sys.setdefaultencoding(encoding)

Upvotes: 3

Martin Green
Martin Green

Reputation: 1054

Try adding # -*- coding: utf-8 -*- as the first line of your source files. It should make Python behave.

Upvotes: 0

Related Questions