I am trying to type some spanish character into strings in interactive mode. For example, option+e e
to produce 'é' so that I make a statement like string="él"
. This works in the terminal, but in python (any version I have installed, of which I have several) it doesn't. It simply produces nothing. This includes trying to paste in the characters. It's exactly the problem described in this question, specifically the part about pasting and in his comment to Alex's question, where he points out that Alex's answer doesn't address the question of the failure of copy/paste with regards to utf-8 characters. How can I do this?

- 1
- 1

- 10,296
- 18
- 68
- 110
-
What exactly do you mean by "the Python interpreter"? I access the Python shell *through* the terminal - how do you do it? – Daniel Roseman Apr 09 '12 at 18:29
-
I think I meant interactive mode. Changed the question. – pythonic metaphor Apr 09 '12 at 18:31
-
That doesn't answer my question. How are you getting to this thing you're calling "interactive mode"? – Daniel Roseman Apr 09 '12 at 19:11
-
Launch Terminal, type `python3`. – pythonic metaphor Apr 09 '12 at 20:44
2 Answers
The readline
library that Python uses to read interactive input doesn't accept non-ASCII characters sent by the terminal. This means that either your terminal emulator doesn't provide the characters in the UTF-8 encoding, or readline is not configured to accept UTF-8 input.
Fortunately, readline is used by many popular programs, including the bash
shell, so there is plenty of information how to get it working. For example, from this article:
How do I get UTF-8 input to work correctly?
- In the Terminal Inspector:
- In the Emulation section, turn off the Escape non-ASCII characters option.
- In the Display section, choose Unicode (UTF-8) as the Character Set Encoding.
Add the following line to your
.profile:
export LC_CTYPE=en_US.UTF-8
Add the following lines to your
.inputrc
:set meta-flag on set input-meta on set output-meta on set convert-meta off
Apply changes by doing a
source ~/.profile
and asource ~/.inputrc
.

- 141,790
- 18
- 296
- 355
use u
before the string literal, to make it an unicode string.
string = u"El avión cayó del cielo"
if that's not working the problem lies in your editor or terminal, in that case try adding
#coding: utf8
in any of the first 2 lines of your python program and use a proper editor with unicode support.

- 12,540
- 6
- 39
- 65
-
The first suggestion doesn't work, I can't input the character at all into the interpreter. I'm not sure what you mean in the second suggestion. I'm using the terminal app that comes with OS-X and I can input the characters in the shell, but not in python in interactive mode. I've already changed my default encoding to be 'utf-8' in `sitecustomize.py`. – pythonic metaphor Apr 09 '12 at 18:20
-
1Changing the default encoding system wide isn't recommended it may break some libraries. – KurzedMetal Apr 09 '12 at 19:02
-
**Don't** change default encoding. What does `import sys; print(sys.stdout.encoding)` say? – Mark Tolonen Apr 10 '12 at 03:11