Using The Python Interpreter
Using The Python Interpreter
Using The Python Interpreter
python3.8
to the shell. [1] Since the choice of the directory where the interpreter lives is an
installation option, other places are possible; check with your local Python guru or
system administrator. (E.g., /usr/local/python is a popular alternative location.)
On Windows machines where you have installed Python from the Microsoft Store,
the python3.8 command will be available. If you have the py.exe launcher installed,
you can use the py command. See Excursus: Setting environment variables for
other ways to launch Python.
The interpreter operates somewhat like the Unix shell: when called with standard
input connected to a tty device, it reads and executes commands interactively; when
called with a file name argument or with a file as standard input, it reads and
executes a script from that file.
A second way of starting the interpreter is python -c command [arg] ... , which
executes the statement(s) in command, analogous to the shell’s -c option. Since
Python statements often contain spaces or other characters that are special to the
shell, it is usually advised to quote command in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked using
python -m module [arg] ... , which executes the source file for module as if you
had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the script and
enter interactive mode afterwards. This can be done by passing -i before the script.
All command line options are described in Command line and environment.
$ python3.8
Python 3.8 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
To declare an encoding other than the default one, a special comment line should
be added as the first line of the file. The syntax is as follows:
For example, to declare that Windows-1252 encoding is to be used, the first line of
your source code file should be:
One exception to the first line rule is when the source code starts with a UNIX
“shebang” line. In this case, the encoding declaration should be added as the
second line of the file. For example:
#!/usr/bin/env python3
# -*- coding: cp1252 -*-
Footnotes
[1] On Unix, the Python 3.x interpreter is by default not installed with the executable
named python , so that it does not conflict with a simultaneously installed
Python 2.x executable.