python3.rst
This file
Path
docs/python3.rst
151 lines · 6.1 KiB
Last changed by
Merge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
History of this fileTake it
Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 1–6why, one step back1Python 3 Support2================34.. currentmodule:: click56Click supports Python 3 but like all other command line utility libraries,Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
lines 7–8why, one step back7it suffers from the Unicode text model in Python 3. All examples in the8documentation were written so that they run on both Python 2.x andAdded notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 9–11why, one step back9Python 3.3 or higher.1011At the moment the strong recommendation is to use Python 2 for theseMerge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
line 12why, one step back12utilities unless Python 3 is a hard requirement.Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 13–21why, one step back1314.. _python3-limitations:1516Python 3 Limitations17--------------------1819At the moment click suffers from a few problems on Python 3:2021* The command line in Unix traditionally is in bytes and not unicode.Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a
line 22why, one step back22 While there are encoding hints for all of this, there are generallyAdded notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
line 23why, one step back23 some situations where this can break. The most common one is SSHMerge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
line 24why, one step back24 connections to machines with different locales.Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
line 25why, one step back25Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
lines 26–27why, one step back26 Misconfigured environments can currently cause a wide range of unicode27 problems on Python 3 due to the lack of support for roundtrippingMerge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
line 28why, one step back28 surrogate escapes. This will not be fixed in click itself!Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
line 29why, one step back29Added notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 30–31why, one step back30 For more information see :ref:`python3-surrogates`.31Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 32–34why, one step back32* Standard input and output on 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, thisMerge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
line 35why, one step back35 might not always work. Primarily this can become a problem whenMerge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a
line 36why, one step back36 testing command-line applications.Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 37–46why, one step back3738 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'))Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
line 47why, one step back47 sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 48–50why, one step back48 out_stream = io.BytesIO()49 sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')50Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
line 51why, one step back51 Remember that in that case you need to use ``out_stream.getvalue()``Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 52–63why, one step back52 and not ``sys.stdout.getvalue()`` if you want to access the buffer53 contents as the wrapper will not forward that method.5455Python 2 / 3 Differences56------------------------5758Click attempts to minimize the differences between Python 2 and Python 359by following the best practices for both languages.6061On Python 2 the following is true:6263* ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` are opened in binaryMerge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
line 64why, one step back64 mode but under some circumstances they support unicode output. ClickAdded notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
line 65why, one step back65 attempts to not subvert this but provides support for forcing streamsMerge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
lines 66–67why, one step back66 to be unicode-based.67* ``sys.argv`` is always bytes-based. Click will pass bytes to allAdded notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 68–71why, one step back68 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.71* When dealing with files, click will never go through the unicode APIsMerge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
lines 72–73why, one step back72 and will instead use the operating system's byte APIs to open the73 files.Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 74–76why, one step back7475On Python 3 the following is true:76Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
line 77why, one step back77* ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are by defaultMerge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a
line 78why, one step back78 text-based. When click needs a binary stream, it attempts to discoverMerge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4
lines 79–81why, one step back79 the underlying binary stream. See :ref:`python3-limitations` for how80 this works.81* ``sys.argv`` is always unicode-based. This also means that the nativeAdded notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 82–86why, one step back82 type for input values to the types in click is unicode and not bytes.8384 This causes problems when the terminal is incorrectly set and Python85 does not figure out the encoding. In that case the unicode string86 will contain error bytes encoded as surrogate escapes.Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a
line 87why, one step back87* When dealing with files, click will always use the unicode file systemAdded notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
line 88why, one step back88 API calls by using the operating system's reported or guessedMerge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a
line 89why, one step back89 filesystem encoding. Surrogates are supported for filenames, so itAdded notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32
lines 90–91why, one step back90 should be possible to open files through the :class:`File` type even91 if the environment is misconfigured.Added notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 92–98why, one step back9293.. _python3-surrogates:9495Python 3 Surrogate Handling96---------------------------9798Click on Python 3 does all the unicode handling in the standard libraryMerge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
line 99why, one step back99and is subject to its behavior. On Python 2 click does all the unicodeAdded notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 100–101why, one step back100handling itself which means there are differences in error behavior.101Merge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
line 102why, one step back102The most glaring difference is that on Python 2 unicode will "just work"Added notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 103–104why, one step back103but requires extra care on Python 3. The reason for this is that on104Python 3 the encoding detection is done in the interpreter and on LinuxMerge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
line 105why, one step back105and certain other operating systems its encoding handling is problematic.Added notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 106–108why, one step back106107The biggest source of frustration there is that click scripts invoked by108init systems (sysvinit, upstart, systemd, etc.), deployment tools (salt,Merge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
line 109why, one step back109puppet) or cron jobs (cron) will refuse to work unless a unicode locale isAdded notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 110–112why, one step back110exported.111112If click encounters such an environment it will prevent further executionMerge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
line 113why, one step back113to force you to set a locale. This is done because click cannot knowAdded notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 114–135why, one step back114about the state of the system once it's invoked and restore the values115before Python's unicode handling kicked in.116117If you see something like this error on 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 switch123 to Python 2 or consult for http://click.pocoo.org/python3/124 mitigation steps.125126You are dealing with an environment where Python 3 thinks you are127restricted to ASCII data. The solution for 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-8135Merge pull request #45 from julen/doc-fixesArmin Ronacherbefore adoptionabe99ca
lines 136–137why, one step back136If you are on a US machine, ``en_EN.utf-8`` is the encoding of choice. On137some newer linux systems you can also try ``C.UTF-8`` as locale::Added notes on unicode on python 3Armin Ronacherbefore adoption51ebc82
lines 138–151why, one step back138139 export LC_ALL=C.UTF-8140 export LANG=C.UTF-8141142You need to do this before you invoke your Python script. If you are143curious about the reasons for this you can join the discussions in the144Python 3 bug tracker:145146* `ASCII is a bad filesystem default encoding147 <http://bugs.python.org/issue13643#msg149941>`_148* `Use surrogateescape as default error handler149 <http://bugs.python.org/issue19977>`_150* `Python 3 raises Unicode errors in the C locale151 <http://bugs.python.org/issue19846>`_