blob: 7ee9c040467701f2d175b779304718f55b04906e [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
174 ``old``
175 division of int/int and long/long return an int or long (*default*)
176 ``new``
177 new division semantics, i.e. division of int/int and long/long returns a
178 float
179 ``warn``
180 old division semantics with a warning for int/int and long/long
181 ``warnall``
182 old division semantics with a warning for all uses of the division operator
183
184 .. seealso::
185 :file:`Tools/scripts/fixdiv.py`
186 for a use of ``warnall``
187
188 :pep:`238` -- Changing the division operator
189
190
191.. cmdoption:: -S
192
193 Disable the import of the module :mod:`site` and the site-dependent
194 manipulations of :data:`sys.path` that it entails.
195
196
197.. cmdoption:: -t
198
199 Issue a warning when a source file mixes tabs and spaces for indentation in a
200 way that makes it depend on the worth of a tab expressed in spaces. Issue an
201 error when the option is given twice (:option:`-tt`).
202
203
204.. cmdoption:: -u
205
206 Force stdin, stdout and stderr to be totally unbuffered. On systems where it
207 matters, also put stdin, stdout and stderr in binary mode.
208
209 Note that there is internal buffering in :meth:`file.readlines` and
210 :ref:`bltin-file-objects` (``for line in sys.stdin``) which is not influenced
211 by this option. To work around this, you will want to use
212 :meth:`file.readline` inside a ``while 1:`` loop.
213
214 See also :envvar:`PYTHONUNBUFFERED`.
215
216
217.. XXX should the -U option be documented?
218
219.. cmdoption:: -v
220
221 Print a message each time a module is initialized, showing the place
222 (filename or built-in module) from which it is loaded. When given twice
223 (:option:`-vv`), print a message for each file that is checked for when
224 searching for a module. Also provides information on module cleanup at exit.
225 See also :envvar:`PYTHONVERBOSE`.
226
227
228.. cmdoption:: -W arg
229
230 Warning control. Python's warning machinery by default prints warning
231 messages to :data:`sys.stderr`. A typical warning message has the following
232 form::
233
234 file:line: category: message
235
236 By default, each warning is printed once for each source line where it
237 occurs. This option controls how often warnings are printed.
238
239 Multiple :option:`-W` options may be given; when a warning matches more than
240 one option, the action for the last matching option is performed. Invalid
241 :option:`-W` options are ignored (though, a warning message is printed about
242 invalid options when the first warning is issued).
243
244 Warnings can also be controlled from within a Python program using the
245 :mod:`warnings` module.
246
247 The simplest form of argument is one of the following action strings (or a
248 unique abbreviation):
249
250 ``ignore``
251 Ignore all warnings.
252 ``default``
253 Explicitly request the default behavior (printing each warning once per
254 source line).
255 ``all``
256 Print a warning each time it occurs (this may generate many messages if a
257 warning is triggered repeatedly for the same source line, such as inside a
258 loop).
259 ``module``
260 Print each warning only only the first time it occurs in each module.
261 ``once``
262 Print each warning only the first time it occurs in the program.
263 ``error``
264 Raise an exception instead of printing a warning message.
265
266 The full form of argument is::
267
268 action:message:category:module:line
269
270 Here, *action* is as explained above but only applies to messages that match
271 the remaining fields. Empty fields match all values; trailing empty fields
272 may be omitted. The *message* field matches the start of the warning message
273 printed; this match is case-insensitive. The *category* field matches the
274 warning category. This must be a class name; the match test whether the
275 actual warning category of the message is a subclass of the specified warning
276 category. The full class name must be given. The *module* field matches the
277 (fully-qualified) module name; this match is case-sensitive. The *line*
278 field matches the line number, where zero matches all line numbers and is
279 thus equivalent to an omitted line number.
280
281 .. seealso::
282
283 :pep:`230` -- Warning framework
284
285
286.. cmdoption:: -x
287
288 Skip the first line of the source, allowing use of non-Unix forms of
289 ``#!cmd``. This is intended for a DOS specific hack only.
290
291 .. warning:: The line numbers in error messages will be off by one!
292
293
294Related files -- UNIX
295---------------------
296
297These are subject to difference depending on local installation conventions;
298:envvar:`prefix` (``${prefix}``) and :envvar:`exec_prefix` (``${exec_prefix}``)
299are installation-dependent and should be interpreted as for GNU software; they
300may be the same.
301
302For example, on most Linux systems, the default for both is :file:`/usr`.
303
304+-----------------------------------------------+------------------------------------------+
305| File/directory | Meaning |
306+===============================================+==========================================+
307| :file:`{exec_prefix}/bin/python` | Recommended location of the interpreter. |
308+-----------------------------------------------+------------------------------------------+
309| :file:`{prefix}/lib/python{version}`, | Recommended locations of the directories |
310| :file:`{exec_prefix}/lib/python{version}` | containing the standard modules. |
311+-----------------------------------------------+------------------------------------------+
312| :file:`{prefix}/include/python{version}`, | Recommended locations of the directories |
313| :file:`{exec_prefix}/include/python{version}` | containing the include files needed for |
314| | developing Python extensions and |
315| | embedding the interpreter. |
316+-----------------------------------------------+------------------------------------------+
317| :file:`~/.pythonrc.py` | User-specific initialization file loaded |
318| | by the user module; not used by default |
319| | or by most applications. |
320+-----------------------------------------------+------------------------------------------+
321
322
323Environment variables
324---------------------
325
326.. envvar:: PYTHONHOME
327
328 Change the location of the standard Python libraries. By default, the
329 libraries are searched in :file:`{prefix}/lib/python<version>` and
330 :file:`{exec_prefix}/lib/python<version>`, where :file:`{prefix}` and
331 :file:`{exec_prefix}` are installation-dependent directories, both defaulting
332 to :file:`/usr/local`.
333
334 When :envvar:`PYTHONHOME` is set to a single directory, its value replaces
335 both :file:`{prefix}` and :file:`{exec_prefix}`. To specify different values
336 for these, set :envvar:`PYTHONHOME` to :file:`{prefix}:{exec_prefix}``.
337
338
339.. envvar:: PYTHONPATH
340
341 Augments the default search path for module files. The format is the same as
342 the shell's :envvar:`PATH`: one or more directory pathnames separated by
343 colons. Non-existent directories are silently ignored.
344
345 The default search path is installation dependent, but generally begins with
346 :file:`{prefix}/lib/python<version>`` (see :envvar:`PYTHONHOME` above). It
347 is *always* appended to :envvar:`PYTHONPATH`.
348
349 If a script argument is given, the directory containing the script is
350 inserted in the path in front of :envvar:`PYTHONPATH`. The search path can
351 be manipulated from within a Python program as the variable :data:`sys.path`.
352
353
354.. envvar:: PYTHONSTARTUP
355
356 If this is the name of a readable file, the Python commands in that file are
357 executed before the first prompt is displayed in interactive mode. The file
358 is executed in the same namespace where interactive commands are executed so
359 that objects defined or imported in it can be used without qualification in
360 the interactive session. You can also change the prompts :data:`sys.ps1` and
361 :data:`sys.ps2` in this file.
362
363
364.. envvar:: PYTHONY2K
365
366 Set this to a non-empty string to cause the :mod:`time` module to require
367 dates specified as strings to include 4-digit years, otherwise 2-digit years
368 are converted based on rules described in the :mod:`time` module
369 documentation.
370
371
372.. envvar:: PYTHONOPTIMIZE
373
374 If this is set to a non-empty string it is equivalent to specifying the
375 :option:`-O` option. If set to an integer, it is equivalent to specifying
376 :option:`-O` multiple times.
377
378
379.. envvar:: PYTHONDEBUG
380
381 If this is set to a non-empty string it is equivalent to specifying the
382 :option:`-d` option. If set to an integer, it is equivalent to specifying
383 :option:`-d` multiple times.
384
385
386.. envvar:: PYTHONINSPECT
387
388 If this is set to a non-empty string it is equivalent to specifying the
389 :option:`-i` option.
390
391
392.. envvar:: PYTHONUNBUFFERED
393
394 If this is set to a non-empty string it is equivalent to specifying the
395 :option:`-u` option.
396
397
398.. envvar:: PYTHONVERBOSE
399
400 If this is set to a non-empty string it is equivalent to specifying the
401 :option:`-v` option. If set to an integer, it is equivalent to specifying
402 :option:`-v` multiple times.
403
404
405.. envvar:: PYTHONCASEOK
406
407 If this is set, Python ignores case in :keyword:`import` statements. This
408 only works on Windows.
409