17db96d56Sopenharmony_ci.. _tut-using: 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci**************************** 47db96d56Sopenharmony_ciUsing the Python Interpreter 57db96d56Sopenharmony_ci**************************** 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ci.. _tut-invoking: 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ciInvoking the Interpreter 117db96d56Sopenharmony_ci======================== 127db96d56Sopenharmony_ci 137db96d56Sopenharmony_ciThe Python interpreter is usually installed as :file:`/usr/local/bin/python3.11` 147db96d56Sopenharmony_cion those machines where it is available; putting :file:`/usr/local/bin` in your 157db96d56Sopenharmony_ciUnix shell's search path makes it possible to start it by typing the command: 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci.. code-block:: text 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ci python3.11 207db96d56Sopenharmony_ci 217db96d56Sopenharmony_cito the shell. [#]_ Since the choice of the directory where the interpreter lives 227db96d56Sopenharmony_ciis an installation option, other places are possible; check with your local 237db96d56Sopenharmony_ciPython guru or system administrator. (E.g., :file:`/usr/local/python` is a 247db96d56Sopenharmony_cipopular alternative location.) 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ciOn Windows machines where you have installed Python from the :ref:`Microsoft Store 277db96d56Sopenharmony_ci<windows-store>`, the :file:`python3.11` command will be available. If you have 287db96d56Sopenharmony_cithe :ref:`py.exe launcher <launcher>` installed, you can use the :file:`py` 297db96d56Sopenharmony_cicommand. See :ref:`setting-envvars` for other ways to launch Python. 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ciTyping an end-of-file character (:kbd:`Control-D` on Unix, :kbd:`Control-Z` on 327db96d56Sopenharmony_ciWindows) at the primary prompt causes the interpreter to exit with a zero exit 337db96d56Sopenharmony_cistatus. If that doesn't work, you can exit the interpreter by typing the 347db96d56Sopenharmony_cifollowing command: ``quit()``. 357db96d56Sopenharmony_ci 367db96d56Sopenharmony_ciThe interpreter's line-editing features include interactive editing, history 377db96d56Sopenharmony_cisubstitution and code completion on systems that support the `GNU Readline 387db96d56Sopenharmony_ci<https://tiswww.case.edu/php/chet/readline/rltop.html>`_ library. 397db96d56Sopenharmony_ciPerhaps the quickest check to see whether command line editing is supported is 407db96d56Sopenharmony_cityping :kbd:`Control-P` to the first Python prompt you get. If it beeps, you 417db96d56Sopenharmony_cihave command line editing; see Appendix :ref:`tut-interacting` for an 427db96d56Sopenharmony_ciintroduction to the keys. If nothing appears to happen, or if ``^P`` is 437db96d56Sopenharmony_ciechoed, command line editing isn't available; you'll only be able to use 447db96d56Sopenharmony_cibackspace to remove characters from the current line. 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_ciThe interpreter operates somewhat like the Unix shell: when called with standard 477db96d56Sopenharmony_ciinput connected to a tty device, it reads and executes commands interactively; 487db96d56Sopenharmony_ciwhen called with a file name argument or with a file as standard input, it reads 497db96d56Sopenharmony_ciand executes a *script* from that file. 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ciA second way of starting the interpreter is ``python -c command [arg] ...``, 527db96d56Sopenharmony_ciwhich executes the statement(s) in *command*, analogous to the shell's 537db96d56Sopenharmony_ci:option:`-c` option. Since Python statements often contain spaces or other 547db96d56Sopenharmony_cicharacters that are special to the shell, it is usually advised to quote 557db96d56Sopenharmony_ci*command* in its entirety. 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ciSome Python modules are also useful as scripts. These can be invoked using 587db96d56Sopenharmony_ci``python -m module [arg] ...``, which executes the source file for *module* as 597db96d56Sopenharmony_ciif you had spelled out its full name on the command line. 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ciWhen a script file is used, it is sometimes useful to be able to run the script 627db96d56Sopenharmony_ciand enter interactive mode afterwards. This can be done by passing :option:`-i` 637db96d56Sopenharmony_cibefore the script. 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ciAll command line options are described in :ref:`using-on-general`. 667db96d56Sopenharmony_ci 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci.. _tut-argpassing: 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ciArgument Passing 717db96d56Sopenharmony_ci---------------- 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ciWhen known to the interpreter, the script name and additional arguments 747db96d56Sopenharmony_cithereafter are turned into a list of strings and assigned to the ``argv`` 757db96d56Sopenharmony_civariable in the ``sys`` module. You can access this list by executing ``import 767db96d56Sopenharmony_cisys``. The length of the list is at least one; when no script and no arguments 777db96d56Sopenharmony_ciare given, ``sys.argv[0]`` is an empty string. When the script name is given as 787db96d56Sopenharmony_ci``'-'`` (meaning standard input), ``sys.argv[0]`` is set to ``'-'``. When 797db96d56Sopenharmony_ci:option:`-c` *command* is used, ``sys.argv[0]`` is set to ``'-c'``. When 807db96d56Sopenharmony_ci:option:`-m` *module* is used, ``sys.argv[0]`` is set to the full name of the 817db96d56Sopenharmony_cilocated module. Options found after :option:`-c` *command* or :option:`-m` 827db96d56Sopenharmony_ci*module* are not consumed by the Python interpreter's option processing but 837db96d56Sopenharmony_cileft in ``sys.argv`` for the command or module to handle. 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci.. _tut-interactive: 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ciInteractive Mode 897db96d56Sopenharmony_ci---------------- 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ciWhen commands are read from a tty, the interpreter is said to be in *interactive 927db96d56Sopenharmony_cimode*. In this mode it prompts for the next command with the *primary prompt*, 937db96d56Sopenharmony_ciusually three greater-than signs (``>>>``); for continuation lines it prompts 947db96d56Sopenharmony_ciwith the *secondary prompt*, by default three dots (``...``). The interpreter 957db96d56Sopenharmony_ciprints a welcome message stating its version number and a copyright notice 967db96d56Sopenharmony_cibefore printing the first prompt: 977db96d56Sopenharmony_ci 987db96d56Sopenharmony_ci.. code-block:: shell-session 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci $ python3.11 1017db96d56Sopenharmony_ci Python 3.11 (default, April 4 2021, 09:25:04) 1027db96d56Sopenharmony_ci [GCC 10.2.0] on linux 1037db96d56Sopenharmony_ci Type "help", "copyright", "credits" or "license" for more information. 1047db96d56Sopenharmony_ci >>> 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci.. XXX update for new releases 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ciContinuation lines are needed when entering a multi-line construct. As an 1097db96d56Sopenharmony_ciexample, take a look at this :keyword:`if` statement:: 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci >>> the_world_is_flat = True 1127db96d56Sopenharmony_ci >>> if the_world_is_flat: 1137db96d56Sopenharmony_ci ... print("Be careful not to fall off!") 1147db96d56Sopenharmony_ci ... 1157db96d56Sopenharmony_ci Be careful not to fall off! 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci 1187db96d56Sopenharmony_ciFor more on interactive mode, see :ref:`tut-interac`. 1197db96d56Sopenharmony_ci 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ci.. _tut-interp: 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ciThe Interpreter and Its Environment 1247db96d56Sopenharmony_ci=================================== 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ci 1277db96d56Sopenharmony_ci.. _tut-source-encoding: 1287db96d56Sopenharmony_ci 1297db96d56Sopenharmony_ciSource Code Encoding 1307db96d56Sopenharmony_ci-------------------- 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ciBy default, Python source files are treated as encoded in UTF-8. In that 1337db96d56Sopenharmony_ciencoding, characters of most languages in the world can be used simultaneously 1347db96d56Sopenharmony_ciin string literals, identifiers and comments --- although the standard library 1357db96d56Sopenharmony_cionly uses ASCII characters for identifiers, a convention that any portable code 1367db96d56Sopenharmony_cishould follow. To display all these characters properly, your editor must 1377db96d56Sopenharmony_cirecognize that the file is UTF-8, and it must use a font that supports all the 1387db96d56Sopenharmony_cicharacters in the file. 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ciTo declare an encoding other than the default one, a special comment line 1417db96d56Sopenharmony_cishould be added as the *first* line of the file. The syntax is as follows:: 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci # -*- coding: encoding -*- 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ciwhere *encoding* is one of the valid :mod:`codecs` supported by Python. 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ciFor example, to declare that Windows-1252 encoding is to be used, the first 1487db96d56Sopenharmony_ciline of your source code file should be:: 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci # -*- coding: cp1252 -*- 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ciOne exception to the *first line* rule is when the source code starts with a 1537db96d56Sopenharmony_ci:ref:`UNIX "shebang" line <tut-scripts>`. In this case, the encoding 1547db96d56Sopenharmony_cideclaration should be added as the second line of the file. For example:: 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci #!/usr/bin/env python3 1577db96d56Sopenharmony_ci # -*- coding: cp1252 -*- 1587db96d56Sopenharmony_ci 1597db96d56Sopenharmony_ci.. rubric:: Footnotes 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci.. [#] On Unix, the Python 3.x interpreter is by default not installed with the 1627db96d56Sopenharmony_ci executable named ``python``, so that it does not conflict with a 1637db96d56Sopenharmony_ci simultaneously installed Python 2.x executable. 164