E: The extra appendix
While editting the tutorial, there is some info which didn’t fit in the tutorial, and was moved to the appendix.
When a proper appendix wasn’t available, the information was stuffed here, so that it can later be better organized.
More advanced startup options
An alternative of starting the interpreter is “
python -c command [arg] …”, which executes the statement(s) in command. Since Python statements often contain spaces or other characters that are special to the shell, it is best to quote
command in its entirety with double 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 given the full module filename on the command line.
Note that there is a difference between “
python filename” and “
python <filename“. In the latter case, input requests from the program itself, such as calls to
raw_input(), are satisfied from file. Since this file has already been read until the end by the parser before the program starts executing, the program will encounter end-of-file immediately. In the former case (which is usually what you want) they are satisfied from whatever file or device is connected to standard input of the Python interpreter.
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. (This does not work if the script is read from standard input, for the same reason as explained in the previous paragraph.)
When invoked from a shell or command window, the script name and additional arguments thereafter are passed to the script in the variable
sys.argv. This variable is a list of strings. Its length is at least one; when no script and no arguments are given, the list contains a single empty string. The Python syntax to access the first element of a list
When the script name is given as
'-' (meaning standard input),
sys.argv is set to “-“. When -c command is used,
sys.argv is set to “-c”. When -m module is used,
sys.argv is set to the full name of the located module. Options found after -c command or -m module are left in
sys.argv for the command or module to handle.
You need to
import sys before you can access the
The Interactive Startup File
When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. You can do this by setting an environment variable named PYTHONSTARTUP to the name of a file containing your start-up commands. This is similar to the .profile feature of the Unix shells.
This file is only read in interactive sessions, not when Python reads commands from a script, and not when /dev/tty is given as the explicit source of commands (which otherwise behaves like an interactive session). It is executed in the same namespace where interactive commands are executed, so that objects that it defines or imports can be used without qualification in the interactive session. You can also change the prompts
sys.ps2 in this file.
If you want to read an additional start-up file from the current directory, you can program this in the global start-up file using code like “if os.path.isfile(‘.pythonrc.py’): execfile(‘.pythonrc.py’)”. If you want to use the startup file in a script, you must do this explicitly in the script:
import os filename = os.environ.get('PYTHONSTARTUP') if filename and os.path.isfile(filename): execfile(filename)