Skip to content
Myr

python3.rst

This file

Path

docs/python3.rst

183 lines · 7.4 KiB

Last changed by

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

History of this file

Take it

The walk stopped after 200 commits: lines older than 99e3d8d point at it.

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 1–8why, one step back
1Python 3 Support
2================34.. currentmodule:: click56Click supports Python 3, but like all other command line utility libraries,7it suffers from the Unicode text model in Python 3. All examples in the8documentation were written so that they could run on both Python 2.x and

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

line 9why, one step back
9Python 3.4 or higher.

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

line 10why, one step back
10

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

line 11why, one step back
11At the moment, it is strongly recommended to use Python 2 for Click

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 12–18why, one step back
12utilities unless Python 3 is a hard requirement.
1314.. _python3-limitations:1516Python 3 Limitations17--------------------18

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 19why, one step back
19At the moment, Click suffers from a few problems with Python 3:

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

line 20why, one step back
20

Merge pull request #247 from homeworkprod/patch-1Armin Ronacherbefore adoptione1cbc25

lines 21–23why, one step back
21* The command line in Unix traditionally is in bytes, not Unicode. While
22 there are encoding hints for all of this, there are generally some23 situations where this can break. The most common one is SSH

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 24–27why, one step back
24 connections to machines with different locales.
2526 Misconfigured environments can currently cause a wide range of Unicode27 problems in Python 3 due to the lack of support for roundtripping

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 28why, one step back
28 surrogate escapes. This will not be fixed in Click itself!

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 29–58why, one step back
29
30 For more information see :ref:`python3-surrogates`.3132* Standard input and output in Python 3 is opened in Unicode mode by33 default. Click has to reopen the stream in binary mode in certain34 situations. Because there is no standardized way to do this, this35 might not always work. Primarily this can become a problem when36 testing command-line applications.3738 This is not supported::3940 sys.stdin = io.StringIO('Input here')41 sys.stdout = io.StringIO()4243 Instead you need to do this::4445 input = 'Input here'46 in_stream = io.BytesIO(input.encode('utf-8'))47 sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')48 out_stream = io.BytesIO()49 sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')5051 Remember that in that case, you need to use ``out_stream.getvalue()``52 and not ``sys.stdout.getvalue()`` if you want to access the buffer53 contents as the wrapper will not forward that method.5455Python 2 and 3 Differences56--------------------------5758Click attempts to minimize the differences between Python 2 and Python 3

Merge pull request #247 from homeworkprod/patch-1Armin Ronacherbefore adoptione1cbc25

line 59why, one step back
59by following best practices for both languages.

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 60–70why, one step back
60
61In Python 2, the following is true:6263* ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` are opened in binary64 mode, but under some circumstances they support Unicode output. Click65 attempts to not subvert this but provides support for forcing streams66 to be Unicode-based.67* ``sys.argv`` is always byte-based. Click will pass bytes to all68 input types and convert as necessary. The :class:`STRING` type69 automatically will decode properly the input value into a string by70 trying the most appropriate encodings.

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 71why, one step back
71* When dealing with files, Click will never go through the Unicode APIs

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 72–77why, one step back
72 and will instead use the operating system's byte APIs to open the
73 files.7475In Python 3, the following is true:7677* ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are by default

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 78why, one step back
78 text-based. When Click needs a binary stream, it attempts to discover

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 79–81why, one step back
79 the underlying binary stream. See :ref:`python3-limitations` for how
80 this works.81* ``sys.argv`` is always Unicode-based. This also means that the native

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 82why, one step back
82 type for input values to the types in Click is Unicode, and not bytes.

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

line 83why, one step back
83

Merge pull request #247 from homeworkprod/patch-1Armin Ronacherbefore adoptione1cbc25

line 84why, one step back
84 This causes problems if the terminal is incorrectly set and Python

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 85–86why, one step back
85 does not figure out the encoding. In that case, the Unicode string
86 will contain error bytes encoded as surrogate escapes.

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 87why, one step back
87* When dealing with files, Click will always use the Unicode file system

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 88–98why, one step back
88 API calls by using the operating system's reported or guessed
89 filesystem encoding. Surrogates are supported for filenames, so it90 should be possible to open files through the :class:`File` type even91 if the environment is misconfigured.9293.. _python3-surrogates:9495Python 3 Surrogate Handling96---------------------------9798Click in Python 3 does all the Unicode handling in the standard library

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 99why, one step back
99and is subject to its behavior. In Python 2, Click does all the Unicode

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 100–102why, one step back
100handling itself, which means there are differences in error behavior.
101102The most glaring difference is that in Python 2, Unicode will "just work",

Merge pull request #247 from homeworkprod/patch-1Armin Ronacherbefore adoptione1cbc25

lines 103–105why, one step back
103while in Python 3, it requires extra care. The reason for this is that in
104Python 3, the encoding detection is done in the interpreter, and on Linux105and certain other operating systems, its encoding handling is problematic.

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

line 106why, one step back
106

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 107why, one step back
107The biggest source of frustration is that Click scripts invoked by

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 108–111why, one step back
108init systems (sysvinit, upstart, systemd, etc.), deployment tools (salt,
109puppet), or cron jobs (cron) will refuse to work unless a Unicode locale is110exported.111

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

lines 112–113why, one step back
112If Click encounters such an environment it will prevent further execution
113to force you to set a locale. This is done because Click cannot know

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 114–122why, one step back
114about the state of the system once it's invoked and restore the values
115before Python's Unicode handling kicked in.116117If you see something like this error in Python 3::118119 Traceback (most recent call last):120 ...121 RuntimeError: Click will abort further execution because Python 3 was122 configured to use ASCII as encoding for the environment. Either switch

Merge pull request #247 from homeworkprod/patch-1Armin Ronacherbefore adoptione1cbc25

line 123why, one step back
123 to Python 2 or consult http://click.pocoo.org/python3/ for

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 124–135why, one step back
124 mitigation steps.
125126You are dealing with an environment where Python 3 thinks you are127restricted to ASCII data. The solution to these problems is different128depending on which locale your computer is running in.129130For instance, if you have a German Linux machine, you can fix the problem131by exporting the locale to ``de_DE.utf-8``::132133 export LC_ALL=de_DE.utf-8134 export LANG=de_DE.utf-8135

Merge pull request #265 from MrTact/patch-1Markus Unterwaditzerbefore adoptiona9b0cbc

line 136why, one step back
136If you are on a US machine, ``en_US.utf-8`` is the encoding of choice. On

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 137–141why, one step back
137some newer Linux systems, you could also try ``C.UTF-8`` as the locale::
138139 export LC_ALL=C.UTF-8140 export LANG=C.UTF-8141

Documented wincmdArmin Ronacherbefore adoption7e8da4b

lines 142–144why, one step back
142On some systems it was reported that `UTF-8` has to be written as `UTF8`
143and vice versa. To see which locales are supported you can invoke144``locale -a``::

Added missing moduleArmin Ronacherbefore adoption40705b9

lines 145–147why, one step back
145
146 locale -a147

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

lines 148–158why, one step back
148You need to do this before you invoke your Python script. If you are
149curious about the reasons for this, you can join the discussions in the150Python 3 bug tracker:151152* `ASCII is a bad filesystem default encoding153 <http://bugs.python.org/issue13643#msg149941>`_154* `Use surrogateescape as default error handler155 <http://bugs.python.org/issue19977>`_156* `Python 3 raises Unicode errors in the C locale157 <http://bugs.python.org/issue19846>`_158* `LC_CTYPE=C: pydoc leaves terminal in an unusable state

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 159why, one step back
159 <http://bugs.python.org/issue21398>`_ (this is relevant to Click

Merge branch '2.x-maintenance'Armin Ronacherbefore adoption99e3d8d

line 160why, one step back
160 because the pager support is provided by the stdlib pydoc module)

Warn about the use of unicode_literalsArmin Ronacherbefore adoption5f33770

lines 161–168why, one step back
161
162Unicode Literals163----------------164165Starting with Click 5.0 there will be a warning for the use of the166``unicode_literals`` future import in Python 2. This has been done due to167the negative consequences of this import with regards to unintentionally168causing bugs due to introducing Unicode data to APIs that are incapable of

Added link to example github issue.Armin Ronacherbefore adoption2534940

lines 169–171why, one step back
169handling them. For some examples of this issue, see the discussion on
170this github issue: `python-future#22171<https://github.com/PythonCharmers/python-future/issues/22>`_.

Warn about the use of unicode_literalsArmin Ronacherbefore adoption5f33770

lines 172–183why, one step back
172
173If you use ``unicode_literals`` in any file that defines a Click command174or that invokes a click command you will be given a warning. You are175strongly encouraged to not use ``unicode_literals`` and instead use176explicit ``u`` prefixes for your Unicode strings.177178If you do want to ignore the warning and continue using179``unicode_literals`` on your own peril, you can disable the warning as180follows::181182 import click183 click.disable_unicode_literals_warning = True