To develop software systems in Python, you write text files that contain Python source code and documentation. You can use any text editor...

To develop software systems in Python, you write text files that contain Python source code and documentation. You can use any text editor, including those in Integrated Development Environments (IDEs). You then process the source files with the Python compiler and interpreter. You can do this directly, or within an IDE, or via another program that embeds Python. The Python interpreter also lets you execute Python code interactively, as do IDEs.

The python Program

The Python interpreter program is run as python (it’s named python.exe on Windows). python includes both the interpreter itself and the Python compiler, which is implicitly invoked, as and if needed, on imported modules. Depending on your system, the program may typically have to be in a directory listed in your PATH environment variable. Alternatively, as with any other program, you can give a complete pathname to it at a command (shell) prompt, or in the shell script (or shortcut target, etc.) that runs it.

PEP 397

On Windows, since PEP 397, py.exe, the launcher, installs in the system area of Windows and is therefore guaranteed—barring deliberate further manipulation on your part—to be always on the PATH.

On Windows, press the Windows key and start typing python: “Python 3.5 (command-line)” (if you have installed v3) appears, along with other choices, such as “IDLE (Python GUI).” Alternatively, if you have the py.exe launcher installed (that’s automatic with v3; with v2, you need to install the standalone launcher), at a command prompt, typing py launches Python. If you have more than one version of Python installed, then the most recent v2 gets run, unless you override that default via a config file or a command-line parameter (e.g., py -3).

Environment Variables

Besides PATH, other environment variables affect the python program. Some environment variables have the same effects as options passed to python on the command line, as documented in the next section. Several environment variables provide settings not available via command-line options. The following list covers only the basics of a few frequently used ones; for all details, see the online docs.

PYTHONHOME

The Python installation directory. A lib subdirectory, containing the standard Python library, must exist under this directory. On Unix-like systems, the standard library modules should be in lib/python-2.7 for Python 2.7, lib/python-3.5 for Python 3.5, and so on. If not set, the Python executable uses some heuristics to locate the installation directory.

PYTHONPATH

A list of directories, separated by colons on Unix-like systems, and by semicolons on Windows. Python can import modules from these directories. This list extends the initial value for Python’s sys.path variable.

PYTHONSTARTUP

The name of a Python source file to run each time an interactive interpreter session starts. No such file runs if this variable is not set or if it is set to the path of a file that is not found. The PYTHONSTARTUP file does not run when you run a Python script; it runs only when you start an interactive session.

How to set and examine environment variables depends on your operating system. In Unix, use shell commands, often within persistent startup shell scripts. On Windows, press the Windows key and start typing environment var and a couple of shortcuts appear, one for user env vars, the other for system ones. On a Mac, you can work like in other Unix-like systems, but there are other options, including a MacPython-specific IDE. For more information about Python on the Mac, see Using Python on a Mac (v2) or Using Python on a Mac (v3).

Command-Line Syntax and Options

The Python interpreter command-line syntax can be summarized as follows:

[path]python {options} [-c command | -m module | file | -] {args}

Here, brackets ([]) enclose what’s optional, braces ({}) enclose items of which zero or more may be present, and vertical bars (|) mean a choice among alternatives. Python consistently uses a slash (/) for file paths, as in Unix.

Running a Python script at a command line can be as simple as:

$ python hello.py
Hello World

If the script is not in the current working directory, provide the path to the script:

$ python ./hello/hello.py
Hello World

The filename of the script can be any absolute or relative file path, and need not have any specific extension (though it is conventional to use a .py extension). Each operating system has its own ways to make the Python scripts themselves executable, but we do not cover those details here.

Options are case-sensitive short strings, starting with a hyphen, that ask python for a nondefault behavior. python accepts only options that start with a hyphen (-), not with a slash. We list the most frequently used options in Table 2-1. Each option’s description gives the environment variable (if any) that, when set, requests the same behavior. Many options have longer versions beginning with two hyphens, as documented in the help available from python -h. For all details, see the online docs.

Table 2-1. Python frequently used command-line options

 Option  Meaning (and equivalent environment variable, if any)
 -B  Don’t save compiled bytecode files to disk (PYTHONDONTWRITEBYTECODE)
 -c  Specifies Python statements as part of the command line
 -E  Ignores all environment variables
 -h  Prints a full list of options and summary help, then terminates
 -i  Runs an interactive session after the file or command runs (PYTHONINSPECT)
 -m  Specifies a Python module or package to run as the main script
 -O  Optimizes generated bytecode (PYTHONOPTIMIZE)—note that this is an uppercase letter O, not the digit 0
 -OO  Like -O, but also removes documentation strings from the bytecode
 -Q arg  Controls the behavior of division operator / on integers (v2 only)
 -S  Omits the implicit import site on startup
 -t  Issues warnings about inconsistent tab usage (-tt issues errors)
 -u  Uses unbuffered binary files for standard output and standard error (PYTHONUNBUFFERED)
 -v  Verbosely traces module import and cleanup actions (PYTHONVERBOSE)
 -V  Prints the Python version number, then terminates
 -W arg  Adds an entry to the warnings filter
 -x  Excludes (skips) the first line of the main script’s source

 

Use -i when you want to get an interactive session immediately after running some script, with top-level variables still intact and available for inspection. You do not need -i for normal interactive sessions, although it does no harm. -t and -tt ensure that you use tabs and spaces consistently in Python sources.

-O and -OO yield small savings of time and space in bytecode generated for modules you import, turning assert statements into no-operations. -OO also discards documentation strings. In v2, only, -Q determines the behavior of division operator / when used between two integer operands. -W adds an entry to the warnings filter.

In v2, -u forces binary mode for standard input, output, and error (not for other file objects). Some platforms, mostly Windows, distinguish binary and text modes. You need binary mode to emit binary data to standard output. -u also ensures that such output happens immediately, without buffering. You need this when delays due to buffering could cause problems, as in some Unix pipelines. In v3, -u forces unbuffered output for standard output’s and error’s binary layer (available from their .buffer attribute), not their text layer.

After the options, if any, comes an indication of which Python script to run. A file path is that of a Python source or bytecode file to run, complete with file extension, if any. On any platform, you may use a slash (/) as the separator between components in this path. On Windows only, you may alternatively use a backslash (\). Instead of a file path, you can use -c command to execute a Python code string command. command normally contains spaces, so you need quotes around it to satisfy your operating system’s shell or command-line processor. Some shells (e.g., bash) let you enter multiple lines as a single argument so that command can be a series of Python statements. Other shells (e.g., Windows shells) limit you to a single line; command can then be one or more simple statements separated by semicolons (;).

Another way to specify which Python script to run is -m module. This option tells Python to load and run a module named module (or the __main__.py member of a package or ZIP file named module) from some directory that is part of Python’s sys.path; this is useful with several modules from Python’s standard library. For example, -m timeit is often the best way to perform micro-benchmarking of Python statements and expressions.

A hyphen, or the lack of any token in this position, tells the interpreter to read program source from standard input — normally, an interactive session. You need an explicit hyphen only if arguments follow. args are arbitrary strings; the Python application being run can access these strings as items of the list sys.argv.

For example, on a standard Windows installation, you can enter the following at a command prompt to have Python print the current date and time:

C:\> py -c "import time; print(time.asctime())"

On Cygwin, Linux, OpenBSD, macOS, and other Unix-like systems, with a default installation of Python from sources, enter the following at a shell prompt to start an interactive session with verbose tracing of module import and cleanup:

$ /usr/local/bin/python -v

You can start the command with just python (you do not have to specify the full path to the Python executable) if the directory of the Python executable is in your PATH environment variable. (If you have multiple versions of Python installed, you can specify the version, with, for example, python2, python2.7, python3, or python3.5, as appropriate; in this case, the version used if you just say python is generally the one you installed most recently.)

Interactive Sessions

When you run python without a script argument, Python starts an interactive session and prompts you to enter Python statements or expressions. Interactive sessions are useful to explore, to check things out, and to use Python as a powerful, extensible interactive calculator.

When you enter a complete statement, Python executes it. When you enter a complete expression, Python evaluates it. If the expression has a result, Python outputs a string representing the result and also assigns the result to the variable named _ (a single underscore) so that you can immediately use that result in another expression. The prompt string is >>> when Python expects a statement or expression and ... when a statement or expression has been started but not completed. In particular, Python prompts you with ... when you have opened a parenthesis on a previous line and have not closed it yet.

You terminate an interactive session with an end-of-file on standard input (Ctrl-Z on Windows, Ctrl-D on Unix-like systems). The statement raise SystemExit also ends the session, as does a call to sys.exit(), either interactively or in code being run.

Line-editing and history facilities depend in part on how Python was built: if the optional readline module was included, the features of the GNU readline library are available. Windows has a simple but usable history facility for interactive textmode programs like python. You can use other line-editing and history facilities by installing pyreadline for Windows, or pyrepl for Unix.

In addition to the built-in Python interactive environment, and those offered as part of richer development environments covered in the next section, you can freely download other alternative, powerful interactive environments. The most popular one is IPython, which offers a dazzling wealth of features. A simpler, lighter-weight, but still quite handy up-and-coming alternative read-line interpreter is bpython.

Python Development Environments

The Python interpreter’s built-in interactive mode is the simplest development environment for Python. It is a bit primitive, but it is lightweight, has a small footprint, and starts fast. Together with an appropriate text editor, and line-editing and history facilities, the interactive interpreter (or, alternatively, the much more powerful IPython/Jupyter command-line interpreter) offers a usable and popular development environment. However, there are a number of other development environments that you can also use.

IDLE

Python’s Integrated DeveLopment Environment (IDLE) comes with the standard Python distribution on most platforms. IDLE is a cross-platform, 100 percent pure Python application based on the Tkinter GUI toolkit. IDLE offers a Python shell similar to interactive Python interpreter sessions but richer in functionality. It also includes a text editor optimized to edit Python source code, an integrated interactive debugger, and several specialized browsers/viewers.

For even more functionality in IDLE, you can download and install IdleX, a substantial collection of free third-party extensions to it.

To install and use IDLE in macOS, follow these specific instructions.

Other Python IDEs

IDLE is mature, stable, easy to use, fairly rich in functionality, and extensible. There are, however, many other IDEs — cross-platform and platform-specific, free and commercial (including commercial IDEs with free offerings, especially if you’re developing open source software yourself), standalone and add-ons to other IDEs.

Some of these IDEs sport features such as static analysis, GUI builders, debuggers, and so on. Python’s IDE wiki page lists over 30 of them, and points to over a dozen external pages with reviews and comparisons. If you’re an IDE collector, happy hunting!

We can’t do justice to even a tiny subset of those IDEs, but it’s worth singling out the popular cross-platform, cross-language modular IDE Eclipse: the free third-party plug-in PyDev for Eclipse has excellent Python support. Steve is a long-time user of Wingware IDE by Archaeopteryx, the most venerable Python-specific IDE. The most popular third-party Python IDE today may be PyCharm.

If you use Visual Studio, check out PTVS, an open source plug-in that’s particularly good at allowing mixed-language debugging in Python and C as and when needed.

Free Text Editors with Python Support

You can edit Python source code with any text editor, even simplistic ones such as Notepad on Windows or ed on Linux. Powerful free editors support Python with extra features such as syntax-based colorization and automatic indentation. Cross-platform editors let you work in uniform ways on different platforms. Good programmers’ text editors also let you run, from within the editor, tools of your choice on the source code you’re editing. An up-to-date list of editors for Python can be found on the Python wiki, which currently lists many dozens of them.

The very best for sheer editing power is probably still classic Emacs (see the Python wiki for many Python-specific add-ons). However, Emacs is not easy to learn, nor is it lightweight. Alex’s personal favorite is another classic, vim, Bram Moolenaar’s modern, improved version of the traditional Unix editor vi, perhaps not quite as powerful as Emacs, but still well worth considering — fast, lightweight, Python-programmable, runs everywhere in both text-mode and GUI versions. See the Python wiki for Python-specific tips and add-ons. Steve and Anna also use vim. When it’s available, Steve also uses the commercial editor Sublime Text 2, with good syntax coloring and enough integration to run your programs from inside the editor.

Tools for Checking Python Programs

The Python compiler does not check programs and modules thoroughly: the compiler checks only the code’s syntax. If you want more thorough checking of your Python code, you may download and install third-party tools for the purpose. Pyflakes is a very fast, lightweight checker: it’s not thorough, but does not import the modules it’s checking, which makes using it safer. At the other end of the spectrum, PyLint is very powerful and highly configurable. PyLint is not lightweight, but repays that by being able to check many style details in a highly configurable way based on customizable configuration files.

Running Python Programs

Whatever tools you use to produce your Python application, you can see your application as a set of Python source files, which are normal text files. A script is a file that you can run directly. A module is a file that you can import to provide functionality to other files or to interactive sessions. A Python file can be both a module (exposing functionality when imported) and a script (suitable for being run directly). A useful and widespread convention is that Python files that are primarily intended to be imported as modules, when run directly, should execute some simple self-test operations.

The Python interpreter automatically compiles Python source files as needed. Python source files normally have the extension .py. In v2, Python saves the compiled bytecode file for each module in the same directory as the module’s source, with the same basename and extension .pyc (or .pyo when you run Python with option -O). In v3, Python saves the compiled bytecode in subdirectory __pycache__ of the directory containing the module’s source, with a version-specific extension, and annotated to denote optimization level.

Run Python with option -B to avoid saving compiled bytecode to disk, which can be handy when you import modules from a read-only disk. Also, Python does not save the compiled bytecode form of a script when you run the script directly; rather, Python recompiles the script each time you run it. Python saves bytecode files only for modules you import. It automatically rebuilds each module’s bytecode file whenever necessary — for example, when you edit the module’s source. Eventually, for deployment, you may package Python modules using tools covered in later chapter.

You can run Python code interactively with the Python interpreter or an IDE. Normally, however, you initiate execution by running a top-level script. To run a script, give its path as an argument to python, as covered earlier. Depending on your operating system, you can invoke python directly from a shell script or command file. On Unix-like systems, you can make a Python script directly executable by setting the file’s permission bits x and r and beginning the script with a so-called shebang line, which is a first line such as:

#!/usr/bin/env python

or some other line starting with #! followed by a path to the python interpreter program, in which case you can optionally add a single word of options, for example:

#!/usr/bin/python -O

On Windows, you can now use the same style #! line, in accordance with PEP 397, to specify a particular version of Python, so your scripts can be cross-platform between Unix-like and Windows systems. You can also run Python scripts with the usual Windows mechanisms, such as double-clicking their icons. When you run a Python script by double-clicking the script’s icon, Windows automatically closes the text-mode console associated with the script as soon as the script terminates. If you want the console to linger (to allow the user to read the script’s output on the screen), ensure the script doesn’t terminate too soon. For example, use the following (in v3) as the script’s last statement:

input('Press Enter to terminate')

(use raw_input in v2). This is not necessary when you run the script from a command prompt.

On Windows, you can also use extension .pyw and interpreter program pythonw.exe instead of .py and python.exe. The w variants run Python without a text-mode console, and thus without standard input and output. These variants are useful for scripts that rely on GUIs or run invisibly in the background. Use them only when a program is fully debugged, to keep standard output and error available for information, warnings, and error messages during development. On a Mac, you need to use interpreter program pythonw, rather than python, when you want to run a script that needs to access any GUI toolkit, rather than just text-mode interaction.

Applications coded in other languages may embed Python, controlling the execution of Python for their own purposes. We examine this briefly in later chapter.

The jython Interpreter

The jython interpreter built during installation is run similarly to the python program:

[path]jython {options} [ -j jar | -c command | file | - ] {args}

-j jar tells Jython that the main script to run is __run__.py in the .jar file. Options -i, -S, and -v are the same as for python. --help is like python’s -h, and --version is like python’s -V. Instead of environment variables, jython uses a text file named registry in the installation directory to record properties with structured names. Property python.path, for example, is the Jython equivalent of Python’s environment variable PYTHONPATH. You can also set properties with jython command-line options in the form -D name=value.

See the Jython homepage for complete, up-to-date information.

The IronPython Interpreter

IronPython may be run similarly to the python program:

[path]ipy {options} [-c command | file | - ] {args}

See the IronPython homepage for complete, up-to-date information.

The PyPy Interpreter

PyPy may be run similarly to the python program:

[path]pypy {options} [-c command | file | - ] {args}

See the PyPy homepage for complete, up-to-date information.

Next And Prev

Next: Python Lexical Structure

Prev: Python Installation

Relate article

Python Installation

Python Lexical Structure

Introduction to Python

Variables and Other References

Python Data Types