blob: 18e6c6826a9195df593f29360e6d230f5d8ffcfe [file] [log] [blame]
Georg Brandl3c8ce772007-11-01 20:58:08 +00001.. highlightlang:: none
2
3Command line and environment
4============================
5
6The CPython interpreter scans the command line and the environment for various
7settings.
8
9.. note::
10
11 Other implementation's command line schemes may differ. See
12 :ref:`implementations` for further resources.
13
14
15Command line
16------------
17
18When invoking Python, you may specify any of these options::
19
20 python [-dEiOQStuUvxX?] [-c command | -m module-name | script | - ] [args]
21
22The most common use case is, of course, a simple invocation of a script::
23
24 python myscript.py
25
26
27Interface options
28~~~~~~~~~~~~~~~~~
29
30The interpreter interface resembles that of the UNIX shell:
31
32* When called with standard input connected to a tty device, it prompts for
33 commands and executes them until an EOF (an end-of-file character, you can
34 produce that with *Ctrl-D* on UNIX or *Ctrl-Z, Enter* on Windows) is read.
35* When called with a file name argument or with a file as standard input, it
36 reads and executes a script from that file.
37* When called with ``-c command``, it executes the Python statement(s) given as
38 *command*. Here *command* may contain multiple statements separated by
39 newlines. Leading whitespace is significant in Python statements!
40* When called with ``-m module-name``, the given module is searched on the
41 Python module path and executed as a script.
42
43In non-interactive mode, the entire input is parsed before it is executed.
44
45An interface option terminates the list of options consumed by the interpreter,
46all consecutive arguments will end up in :data:`sys.argv` -- note that the first
47element, subscript zero (``sys.argv[0]``), is a string reflecting the program's
48source.
49
50.. cmdoption:: -c <command>
51
52 Execute the Python code in *command*. *command* can be one ore more
53 statements separated by newlines, with significant leading whitespace as in
54 normal module code.
55
56 If this option is given, the first element of :data:`sys.argv` will be
57 ``"-c"``.
58
59
60.. cmdoption:: -m <module-name>
61
62 Search :data:`sys.path` for the named module and run the corresponding module
63 file as if it were executed with ``python modulefile.py`` as a script.
64
65 Since the argument is a *module* name, you must not give a file extension
66 (``.py``). However, the ``module-name`` does not have to be a valid Python
67 identifer (e.g. you can use a file name including a hyphen).
68
69 .. note::
70
71 This option cannot be used with builtin modules and extension modules
72 written in C, since they do not have Python module files.
73
74 If this option is given, the first element of :data:`sys.argv` will be the
75 full path to the module file.
76
77 Many standard library modules contain code that is invoked on their execution
78 as a script. An example is the :mod:`timeit` module::
79
80 python -mtimeit -s 'setup here' 'benchmarked code here'
81 python -mtimeit -h # for details
82
83 .. seealso::
84 :func:`runpy.run_module`
85 The actual implementation of this feature.
86
87 :pep:`338` -- Executing modules as scripts
88
89
90.. describe:: <script>
91
92 Execute the Python code contained in *script*, which must be an (absolute or
93 relative) file name.
94
95 If this option is given, the first element of :data:`sys.argv` will be the
96 script file name as given on the command line.
97
98
99.. describe:: -
100
101 Read commands from standard input (:data:`sys.stdin`). If standard input is
102 a terminal, :option:`-i` is implied.
103
104 If this option is given, the first element of :data:`sys.argv` will be
105 ``"-"``.
106
107 .. seealso::
108 :ref:`tut-invoking`
109
110
111If no script name is given, ``sys.argv[0]`` is an empty string (``""``).
112
113
114Generic options
115~~~~~~~~~~~~~~~
116
117.. cmdoption:: -?
118 -h
119 --help
120
121 Print a short description of all command line options.
122
123
124.. cmdoption:: -V
125 --version
126
127 Print the Python version number and exit. Example output could be::
128
129 Python 2.5.1
130
131
132Miscellaneous options
133~~~~~~~~~~~~~~~~~~~~~
134
135.. cmdoption:: -d
136
137 Turn on parser debugging output (for wizards only, depending on compilation
138 options). See also :envvar:`PYTHONDEBUG`.
139
140
141.. cmdoption:: -E
142
143 Ignore all :envvar:`PYTHON*` environment variables, e.g.
144 :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set.
145
146
147.. cmdoption:: -i
148
149 When a script is passed as first argument or the :option:`-c` option is used,
150 enter interactive mode after executing the script or the command, even when
151 :data:`sys.stdin` does not appear to be a terminal. The
152 :envvar:`PYTHONSTARTUP` file is not read.
153
154 This can be useful to inspect global variables or a stack trace when a script
155 raises an exception. See also :envvar:`PYTHONINSPECT`.
156
157
158.. cmdoption:: -O
159
160 Turn on basic optimizations. This changes the filename extension for
161 compiled (:term:`bytecode`) files from ``.pyc`` to ``.pyo``. See also
162 :envvar:`PYTHONOPTIMIZE`.
163
164
165.. cmdoption:: -OO
166
167 Discard docstrings in addition to the :option:`-O` optimizations.
168
169
170.. cmdoption:: -Q <arg>
171
172 Division control. The argument must be one of the following:
173
Georg Brandl3c8ce772007-11-01 20:58:08 +0000174 ``new``
Georg Brandlba956ae2007-11-29 17:24:34 +0000175 new division semantics, i.e. division of int/int returns a float (*default*)
176 ``old``
177 division of int/int returns an int
Georg Brandl3c8ce772007-11-01 20:58:08 +0000178 ``warn``
Georg Brandlba956ae2007-11-29 17:24:34 +0000179 old division semantics with a warning for int/int
Georg Brandl3c8ce772007-11-01 20:58:08 +0000180 ``warnall``
181 old division semantics with a warning for all uses of the division operator
182
183 .. seealso::
184 :file:`Tools/scripts/fixdiv.py`
185 for a use of ``warnall``
186
187 :pep:`238` -- Changing the division operator
188
189
190.. cmdoption:: -S
191
192 Disable the import of the module :mod:`site` and the site-dependent
193 manipulations of :data:`sys.path` that it entails.
194
195
196.. cmdoption:: -t
197
198 Issue a warning when a source file mixes tabs and spaces for indentation in a
199 way that makes it depend on the worth of a tab expressed in spaces. Issue an
200 error when the option is given twice (:option:`-tt`).
201
202
203.. cmdoption:: -u
204
205 Force stdin, stdout and stderr to be totally unbuffered. On systems where it
206 matters, also put stdin, stdout and stderr in binary mode.
207
208 Note that there is internal buffering in :meth:`file.readlines` and
209 :ref:`bltin-file-objects` (``for line in sys.stdin``) which is not influenced
210 by this option. To work around this, you will want to use
211 :meth:`file.readline` inside a ``while 1:`` loop.
212
213 See also :envvar:`PYTHONUNBUFFERED`.
214
215
216.. XXX should the -U option be documented?
217
218.. cmdoption:: -v
219
220 Print a message each time a module is initialized, showing the place
221 (filename or built-in module) from which it is loaded. When given twice
222 (:option:`-vv`), print a message for each file that is checked for when
223 searching for a module. Also provides information on module cleanup at exit.
224 See also :envvar:`PYTHONVERBOSE`.
225
226
227.. cmdoption:: -W arg
228
229 Warning control. Python's warning machinery by default prints warning
230 messages to :data:`sys.stderr`. A typical warning message has the following
231 form::
232
233 file:line: category: message
234
235 By default, each warning is printed once for each source line where it
236 occurs. This option controls how often warnings are printed.
237
238 Multiple :option:`-W` options may be given; when a warning matches more than
239 one option, the action for the last matching option is performed. Invalid
240 :option:`-W` options are ignored (though, a warning message is printed about
241 invalid options when the first warning is issued).
242
243 Warnings can also be controlled from within a Python program using the
244 :mod:`warnings` module.
245
246 The simplest form of argument is one of the following action strings (or a
247 unique abbreviation):
248
249 ``ignore``
250 Ignore all warnings.
251 ``default``
252 Explicitly request the default behavior (printing each warning once per
253 source line).
254 ``all``
255 Print a warning each time it occurs (this may generate many messages if a
256 warning is triggered repeatedly for the same source line, such as inside a
257 loop).
258 ``module``
259 Print each warning only only the first time it occurs in each module.
260 ``once``
261 Print each warning only the first time it occurs in the program.
262 ``error``
263 Raise an exception instead of printing a warning message.
264
265 The full form of argument is::
266
267 action:message:category:module:line
268
269 Here, *action* is as explained above but only applies to messages that match
270 the remaining fields. Empty fields match all values; trailing empty fields
271 may be omitted. The *message* field matches the start of the warning message
272 printed; this match is case-insensitive. The *category* field matches the
273 warning category. This must be a class name; the match test whether the
274 actual warning category of the message is a subclass of the specified warning
275 category. The full class name must be given. The *module* field matches the
276 (fully-qualified) module name; this match is case-sensitive. The *line*
277 field matches the line number, where zero matches all line numbers and is
278 thus equivalent to an omitted line number.
279
280 .. seealso::
281
282 :pep:`230` -- Warning framework
283
284
285.. cmdoption:: -x
286
287 Skip the first line of the source, allowing use of non-Unix forms of
288 ``#!cmd``. This is intended for a DOS specific hack only.
289
290 .. warning:: The line numbers in error messages will be off by one!
291
292
293Related files -- UNIX
294---------------------
295
296These are subject to difference depending on local installation conventions;
297:envvar:`prefix` (``${prefix}``) and :envvar:`exec_prefix` (``${exec_prefix}``)
298are installation-dependent and should be interpreted as for GNU software; they
299may be the same.
300
301For example, on most Linux systems, the default for both is :file:`/usr`.
302
303+-----------------------------------------------+------------------------------------------+
304| File/directory | Meaning |
305+===============================================+==========================================+
306| :file:`{exec_prefix}/bin/python` | Recommended location of the interpreter. |
307+-----------------------------------------------+------------------------------------------+
308| :file:`{prefix}/lib/python{version}`, | Recommended locations of the directories |
309| :file:`{exec_prefix}/lib/python{version}` | containing the standard modules. |
310+-----------------------------------------------+------------------------------------------+
311| :file:`{prefix}/include/python{version}`, | Recommended locations of the directories |
312| :file:`{exec_prefix}/include/python{version}` | containing the include files needed for |
313| | developing Python extensions and |
314| | embedding the interpreter. |
315+-----------------------------------------------+------------------------------------------+
316| :file:`~/.pythonrc.py` | User-specific initialization file loaded |
317| | by the user module; not used by default |
318| | or by most applications. |
319+-----------------------------------------------+------------------------------------------+
320
321
322Environment variables
323---------------------
324
325.. envvar:: PYTHONHOME
326
327 Change the location of the standard Python libraries. By default, the
328 libraries are searched in :file:`{prefix}/lib/python<version>` and
329 :file:`{exec_prefix}/lib/python<version>`, where :file:`{prefix}` and
330 :file:`{exec_prefix}` are installation-dependent directories, both defaulting
331 to :file:`/usr/local`.
332
333 When :envvar:`PYTHONHOME` is set to a single directory, its value replaces
334 both :file:`{prefix}` and :file:`{exec_prefix}`. To specify different values
335 for these, set :envvar:`PYTHONHOME` to :file:`{prefix}:{exec_prefix}``.
336
337
338.. envvar:: PYTHONPATH
339
340 Augments the default search path for module files. The format is the same as
341 the shell's :envvar:`PATH`: one or more directory pathnames separated by
342 colons. Non-existent directories are silently ignored.
343
344 The default search path is installation dependent, but generally begins with
345 :file:`{prefix}/lib/python<version>`` (see :envvar:`PYTHONHOME` above). It
346 is *always* appended to :envvar:`PYTHONPATH`.
347
348 If a script argument is given, the directory containing the script is
349 inserted in the path in front of :envvar:`PYTHONPATH`. The search path can
350 be manipulated from within a Python program as the variable :data:`sys.path`.
351
352
353.. envvar:: PYTHONSTARTUP
354
355 If this is the name of a readable file, the Python commands in that file are
356 executed before the first prompt is displayed in interactive mode. The file
357 is executed in the same namespace where interactive commands are executed so
358 that objects defined or imported in it can be used without qualification in
359 the interactive session. You can also change the prompts :data:`sys.ps1` and
360 :data:`sys.ps2` in this file.
361
362
363.. envvar:: PYTHONY2K
364
365 Set this to a non-empty string to cause the :mod:`time` module to require
366 dates specified as strings to include 4-digit years, otherwise 2-digit years
367 are converted based on rules described in the :mod:`time` module
368 documentation.
369
370
371.. envvar:: PYTHONOPTIMIZE
372
373 If this is set to a non-empty string it is equivalent to specifying the
374 :option:`-O` option. If set to an integer, it is equivalent to specifying
375 :option:`-O` multiple times.
376
377
378.. envvar:: PYTHONDEBUG
379
380 If this is set to a non-empty string it is equivalent to specifying the
381 :option:`-d` option. If set to an integer, it is equivalent to specifying
382 :option:`-d` multiple times.
383
384
385.. envvar:: PYTHONINSPECT
386
387 If this is set to a non-empty string it is equivalent to specifying the
388 :option:`-i` option.
389
390
391.. envvar:: PYTHONUNBUFFERED
392
393 If this is set to a non-empty string it is equivalent to specifying the
394 :option:`-u` option.
395
396
397.. envvar:: PYTHONVERBOSE
398
399 If this is set to a non-empty string it is equivalent to specifying the
400 :option:`-v` option. If set to an integer, it is equivalent to specifying
401 :option:`-v` multiple times.
402
403
404.. envvar:: PYTHONCASEOK
405
406 If this is set, Python ignores case in :keyword:`import` statements. This
407 only works on Windows.
408