blob: 8a4a9b7e5b70da065fcfe65c5063a38319d67094 [file] [log] [blame]
Georg Brandl59d121a2007-10-20 18:08:14 +00001.. highlightlang:: none
2
Georg Brandl1cddfed2007-10-20 18:33:20 +00003Command line and environment
4============================
Georg Brandl59d121a2007-10-20 18:08:14 +00005
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 [-dEiOQStuUvxX3?] [-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 .. versionchanged:: 2.5
90 The module name can now include packages.
91
92
93.. describe:: <script>
94
95 Execute the Python code contained in *script*, which must be an (absolute or
96 relative) file name.
97
98 If this option is given, the first element of :data:`sys.argv` will be the
99 script file name as given on the command line.
100
101
102.. describe:: -
103
104 Read commands from standard input (:data:`sys.stdin`). If standard input is
105 a terminal, :option:`-i` is implied.
106
107 If this option is given, the first element of :data:`sys.argv` will be
108 ``"-"``.
109
110 .. seealso::
111 :ref:`tut-invoking`
112
113
114If no script name is given, ``sys.argv[0]`` is an empty string (``""``).
115
116
117Generic options
118~~~~~~~~~~~~~~~
119
120.. cmdoption:: -?
121 -h
122 --help
123
124 Print a short description of all command line options.
125
126 .. versionadded:: 2.5
127 The ``--help`` variant.
128
129
130.. cmdoption:: -V
131 --version
132
133 Print the Python version number and exit. Example output could be::
134
135 Python 2.5.1
136
137 .. versionadded:: 2.5
138 The ``--version`` variant.
139
140
141Miscellaneous options
142~~~~~~~~~~~~~~~~~~~~~
143
144.. cmdoption:: -d
145
146 Turn on parser debugging output (for wizards only, depending on compilation
147 options). See also :envvar:`PYTHONDEBUG`.
148
149
150.. cmdoption:: -E
151
Georg Brandlc5004f32007-10-20 19:05:45 +0000152 Ignore all :envvar:`PYTHON*` environment variables, e.g.
Georg Brandl35073332007-10-20 19:08:36 +0000153 :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set.
Georg Brandl59d121a2007-10-20 18:08:14 +0000154
Georg Brandlc5004f32007-10-20 19:05:45 +0000155 .. versionadded:: 2.2
Georg Brandl59d121a2007-10-20 18:08:14 +0000156
157
158.. cmdoption:: -i
159
160 When a script is passed as first argument or the :option:`-c` option is used,
161 enter interactive mode after executing the script or the command, even when
162 :data:`sys.stdin` does not appear to be a terminal. The
163 :envvar:`PYTHONSTARTUP` file is not read.
164
165 This can be useful to inspect global variables or a stack trace when a script
166 raises an exception. See also :envvar:`PYTHONINSPECT`.
167
168
169.. cmdoption:: -O
170
171 Turn on basic optimizations. This changes the filename extension for
Georg Brandl5e52db02007-10-21 10:45:46 +0000172 compiled (:term:`bytecode`) files from ``.pyc`` to ``.pyo``. See also
Georg Brandl59d121a2007-10-20 18:08:14 +0000173 :envvar:`PYTHONOPTIMIZE`.
174
175
176.. cmdoption:: -OO
177
178 Discard docstrings in addition to the :option:`-O` optimizations.
179
180
181.. cmdoption:: -Q <arg>
182
183 Division control. The argument must be one of the following:
184
185 ``old``
186 division of int/int and long/long return an int or long (*default*)
187 ``new``
188 new division semantics, i.e. division of int/int and long/long returns a
189 float
190 ``warn``
191 old division semantics with a warning for int/int and long/long
192 ``warnall``
193 old division semantics with a warning for all uses of the division operator
194
195 .. seealso::
196 :file:`Tools/scripts/fixdiv.py`
197 for a use of ``warnall``
198
199 :pep:`238` -- Changing the division operator
200
201
202.. cmdoption:: -S
203
204 Disable the import of the module :mod:`site` and the site-dependent
205 manipulations of :data:`sys.path` that it entails.
206
207
208.. cmdoption:: -t
209
210 Issue a warning when a source file mixes tabs and spaces for indentation in a
211 way that makes it depend on the worth of a tab expressed in spaces. Issue an
212 error when the option is given twice (:option:`-tt`).
213
214
215.. cmdoption:: -u
216
217 Force stdin, stdout and stderr to be totally unbuffered. On systems where it
218 matters, also put stdin, stdout and stderr in binary mode.
219
Georg Brandlf4ef23f2007-10-30 17:51:18 +0000220 Note that there is internal buffering in :meth:`file.readlines` and
Georg Brandl59d121a2007-10-20 18:08:14 +0000221 :ref:`bltin-file-objects` (``for line in sys.stdin``) which is not influenced
222 by this option. To work around this, you will want to use
Georg Brandlf4ef23f2007-10-30 17:51:18 +0000223 :meth:`file.readline` inside a ``while 1:`` loop.
Georg Brandl59d121a2007-10-20 18:08:14 +0000224
225 See also :envvar:`PYTHONUNBUFFERED`.
226
227
228.. XXX should the -U option be documented?
229
230.. cmdoption:: -v
231
232 Print a message each time a module is initialized, showing the place
233 (filename or built-in module) from which it is loaded. When given twice
234 (:option:`-vv`), print a message for each file that is checked for when
235 searching for a module. Also provides information on module cleanup at exit.
236 See also :envvar:`PYTHONVERBOSE`.
237
238
239.. cmdoption:: -W arg
240
241 Warning control. Python's warning machinery by default prints warning
242 messages to :data:`sys.stderr`. A typical warning message has the following
243 form::
244
245 file:line: category: message
246
247 By default, each warning is printed once for each source line where it
248 occurs. This option controls how often warnings are printed.
249
250 Multiple :option:`-W` options may be given; when a warning matches more than
251 one option, the action for the last matching option is performed. Invalid
252 :option:`-W` options are ignored (though, a warning message is printed about
253 invalid options when the first warning is issued).
254
255 Warnings can also be controlled from within a Python program using the
256 :mod:`warnings` module.
257
258 The simplest form of argument is one of the following action strings (or a
259 unique abbreviation):
260
261 ``ignore``
262 Ignore all warnings.
263 ``default``
264 Explicitly request the default behavior (printing each warning once per
265 source line).
266 ``all``
267 Print a warning each time it occurs (this may generate many messages if a
268 warning is triggered repeatedly for the same source line, such as inside a
269 loop).
270 ``module``
271 Print each warning only only the first time it occurs in each module.
272 ``once``
273 Print each warning only the first time it occurs in the program.
274 ``error``
275 Raise an exception instead of printing a warning message.
276
277 The full form of argument is::
278
279 action:message:category:module:line
280
281 Here, *action* is as explained above but only applies to messages that match
282 the remaining fields. Empty fields match all values; trailing empty fields
283 may be omitted. The *message* field matches the start of the warning message
284 printed; this match is case-insensitive. The *category* field matches the
285 warning category. This must be a class name; the match test whether the
286 actual warning category of the message is a subclass of the specified warning
287 category. The full class name must be given. The *module* field matches the
288 (fully-qualified) module name; this match is case-sensitive. The *line*
289 field matches the line number, where zero matches all line numbers and is
290 thus equivalent to an omitted line number.
291
292 .. seealso::
293
294 :pep:`230` -- Warning framework
295
296
297.. cmdoption:: -x
298
299 Skip the first line of the source, allowing use of non-Unix forms of
300 ``#!cmd``. This is intended for a DOS specific hack only.
301
302 .. warning:: The line numbers in error messages will be off by one!
303
304
305.. cmdoption:: -3
306
307 Warn about Python 3.x incompatibilities.
308
309 .. versionadded:: 2.6
310
311
312Related files -- UNIX
313---------------------
314
315These are subject to difference depending on local installation conventions;
316:envvar:`prefix` (``${prefix}``) and :envvar:`exec_prefix` (``${exec_prefix}``)
317are installation-dependent and should be interpreted as for GNU software; they
318may be the same.
319
320For example, on most Linux systems, the default for both is :file:`/usr`.
321
322+-----------------------------------------------+------------------------------------------+
323| File/directory | Meaning |
324+===============================================+==========================================+
325| :file:`{exec_prefix}/bin/python` | Recommended location of the interpreter. |
326+-----------------------------------------------+------------------------------------------+
327| :file:`{prefix}/lib/python{version}`, | Recommended locations of the directories |
328| :file:`{exec_prefix}/lib/python{version}` | containing the standard modules. |
329+-----------------------------------------------+------------------------------------------+
330| :file:`{prefix}/include/python{version}`, | Recommended locations of the directories |
331| :file:`{exec_prefix}/include/python{version}` | containing the include files needed for |
332| | developing Python extensions and |
333| | embedding the interpreter. |
334+-----------------------------------------------+------------------------------------------+
335| :file:`~/.pythonrc.py` | User-specific initialization file loaded |
336| | by the user module; not used by default |
337| | or by most applications. |
338+-----------------------------------------------+------------------------------------------+
339
340
341Environment variables
342---------------------
343
344.. envvar:: PYTHONHOME
345
346 Change the location of the standard Python libraries. By default, the
347 libraries are searched in :file:`{prefix}/lib/python<version>` and
348 :file:`{exec_prefix}/lib/python<version>`, where :file:`{prefix}` and
349 :file:`{exec_prefix}` are installation-dependent directories, both defaulting
350 to :file:`/usr/local`.
351
352 When :envvar:`PYTHONHOME` is set to a single directory, its value replaces
353 both :file:`{prefix}` and :file:`{exec_prefix}`. To specify different values
354 for these, set :envvar:`PYTHONHOME` to :file:`{prefix}:{exec_prefix}``.
355
356
357.. envvar:: PYTHONPATH
358
359 Augments the default search path for module files. The format is the same as
360 the shell's :envvar:`PATH`: one or more directory pathnames separated by
361 colons. Non-existent directories are silently ignored.
362
363 The default search path is installation dependent, but generally begins with
364 :file:`{prefix}/lib/python<version>`` (see :envvar:`PYTHONHOME` above). It
365 is *always* appended to :envvar:`PYTHONPATH`.
366
367 If a script argument is given, the directory containing the script is
368 inserted in the path in front of :envvar:`PYTHONPATH`. The search path can
369 be manipulated from within a Python program as the variable :data:`sys.path`.
370
371
372.. envvar:: PYTHONSTARTUP
373
374 If this is the name of a readable file, the Python commands in that file are
375 executed before the first prompt is displayed in interactive mode. The file
Georg Brandla7395032007-10-21 12:15:05 +0000376 is executed in the same namespace where interactive commands are executed so
Georg Brandl59d121a2007-10-20 18:08:14 +0000377 that objects defined or imported in it can be used without qualification in
378 the interactive session. You can also change the prompts :data:`sys.ps1` and
379 :data:`sys.ps2` in this file.
380
381
382.. envvar:: PYTHONY2K
383
384 Set this to a non-empty string to cause the :mod:`time` module to require
385 dates specified as strings to include 4-digit years, otherwise 2-digit years
386 are converted based on rules described in the :mod:`time` module
387 documentation.
388
389
390.. envvar:: PYTHONOPTIMIZE
391
392 If this is set to a non-empty string it is equivalent to specifying the
393 :option:`-O` option. If set to an integer, it is equivalent to specifying
394 :option:`-O` multiple times.
395
396
397.. envvar:: PYTHONDEBUG
398
399 If this is set to a non-empty string it is equivalent to specifying the
400 :option:`-d` option. If set to an integer, it is equivalent to specifying
401 :option:`-d` multiple times.
402
403
404.. envvar:: PYTHONINSPECT
405
406 If this is set to a non-empty string it is equivalent to specifying the
407 :option:`-i` option.
408
409
410.. envvar:: PYTHONUNBUFFERED
411
412 If this is set to a non-empty string it is equivalent to specifying the
413 :option:`-u` option.
414
415
416.. envvar:: PYTHONVERBOSE
417
418 If this is set to a non-empty string it is equivalent to specifying the
419 :option:`-v` option. If set to an integer, it is equivalent to specifying
420 :option:`-v` multiple times.
421
422
423.. envvar:: PYTHONCASEOK
424
425 If this is set, Python ignores case in :keyword:`import` statements. This
426 only works on Windows.
427